Search code examples
ruby-on-railsdevisegoogle-oauth

How to get omniauth info from Devise


When we configure Devise we put in config/initializers/devise.rb something like this:

  config.omniauth :google_oauth2, "[client_id].apps.googleusercontent.com", "[client_secret]"

I'm curious how to use this info (client_id and client_secret) inside the app?

For example,

flow = Google::APIClient::InstalledAppFlow.new(
  :client_id => client_secrets.client_id,
  :client_secret => client_secrets.client_secret,
  :scope => [YOUTUBE_READONLY_SCOPE]
)

I'd like to get this info from Devise instead of hardcoding it.


Solution

  • You could add the client_id and client_secret in an yml file called google.yml (for example) and in devise.rb you could have something like:

    config_google = YAML.load_file("#{Rails.root}/config/google.yml")
    config.omniauth :google_oauth2, config_google["client_id"], config_google["client_secret"]
    

    Same thing goes if you want to use the config outside the initializer. Just load the yml and use its contents.

    Inside the yml you can have different keys for each environment (development, production, etc). Just make sure you load it properly.

    YAML.load_file("#{Rails.root}/config/google.yml")[Rails.env] # for example