Last Updated: February 25, 2016
·
3.079K
· vyachkonovalov

Getting Devise Omniauth config values in Rails app

Let's say we have Devise config:

/config/initializers/devise.rb

Devise.setup do |config|
  # ...
  config.omniauth :facebook, "App ID/API Key", "App Secret",
          scope: 'email,user_photos,user_relationships,publish_actions',
                  strategy_class: OmniAuth::Strategies::Facebook
  config.omniauth :twitter, "Consumer Key", "Consumer Secret",
                  strategy_class: OmniAuth::Strategies::Twitter
  # ...
end

and we want to read this values in any place of our app. For example, when create Twitter::Client, solution looks like:

auth = user.authentications.where(provider: 'twitter').first # or something like
strategy = Devise.omniauth_configs[:twitter].strategy
twitter = Twitter::Client.new(consumer_key: strategy.consumer_key,
                              consumer_secret: strategy.consumer_secret,
                              oauth_token: auth.token,
                              oauth_token_secret: auth.secret_token)

and for facebook:

strategy = Devise.omniauth_configs[:facebook].strategy
strategy.client_id
strategy.client_secret