user984621
user984621

Reputation: 48521

Rails - Devise+OmniAuth: how to set up credentials for staging version?

In /config/initializers/devise.rb I have something like this:

  # production
  config.omniauth :facebook, 'aaa', 'bbb',
      :site              => 'https://graph.facebook.com',
      :authorize_path    => '/oauth/authorize',
      :access_token_path => '/oauth/access_token',
      :scope => 'email'

  # staging version
  config.omniauth :facebook, 'ccc', 'ddd',
      :site              => 'https://graph.facebook.com',
      :authorize_path    => '/oauth/authorize',
      :access_token_path => '/oauth/access_token',
      :scope => 'email'

When I put these 2 blocks of code into the devise.rb file, I get the error saying that there are incorrect credentials.

I don't know what's the best approach to set up OmniAuth credentials to services like Twitter and Facebook for devise - the one I used is apparently incorrect.

What's the best approach to set up credentials for localhost, production and staging version of an app?

Thanks

Upvotes: 1

Views: 678

Answers (3)

cthulhu
cthulhu

Reputation: 3726

The best way to handle different credentials is to put them in environment variables, just like omniauth gem doc says:

https://github.com/intridea/omniauth#getting-started

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :developer unless Rails.env.production?
  provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
end

This approach has several advantages:

  1. It keeps your configuration simple
  2. No credentials in code repository
  3. It doesn't limit you to dev/test/prod

Upvotes: 0

user3746033
user3746033

Reputation: 1

case Rails.env
when "production"
    # production version
    config.omniauth :facebook, 'aaa', 'bbb',
            :site              => GRAPH_URL,
            :authorize_path    => '/oauth/authorize',
            :access_token_path => '/oauth/access_token',
            :scope => 'email'
when "staging"
    # staging version
    config.omniauth :facebook, 'ccc', 'ddd',
            :site              => GRAPH_URL,
            :authorize_path    => '/oauth/authorize',
            :access_token_path => '/oauth/access_token',
            :scope => 'email'
else
    # development version
    config.omniauth :facebook, 'eee', 'fff',
            :site              => GRAPH_URL,
            :authorize_path    => '/oauth/authorize',
            :access_token_path => '/oauth/access_token',
            :scope => 'email'
end

Upvotes: 0

Voldemar Duletskiy
Voldemar Duletskiy

Reputation: 981

It seems that your credentials are wrong for localhost. I have two versions of creditals for development and production, here is example

if Rails.env.development?
    config.omniauth :facebook, "xxx", "yyy"
    config.omniauth :vkontakte, "xxx_loc", "yyy_loc"
else
    config.omniauth :facebook, "zzz", "rrr"
    config.omniauth :vkontakte, 'zzz_loc', 'rrr_loc'
end

at /config/initializers/devise.rb

Upvotes: 1

Related Questions