Search code examples
ruby-on-rails-4configurationomniauth-facebook

Omniauth configuration in secrets rails 4


I'm trying to configure different environments so I can test Facebook login. Production works fine, but I'm having trouble with the correct way to include my development credentials. I'm trying to use secrets.yml since that is what I've used to store other api credentials.

Here's what I've got in the initializer, omniauth.rb :

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET'],
           :scope => 'email,user_location'
 end

And this in my secrets.yml file:

development:
  FACEBOOK_APP_ID: mytestappid
  FACEBOOK_SECRET: mytestsecretkey

production:
  FACEBOOK_APP_ID: <%= ENV["FACEBOOK_APP_ID"] %>
  FACEBOOK_SECRET: <%= ENV["FACEBOOK_SECRET"] %>

I tried changing the 'config' to 'secrets' in the initializer but that didn't work. I suspect it has something to do with middleware but I'm not sure. Can anyone help point me in the direction of configuring this properly? Production only works bc I set the config variables with heroku.

Thanks a lot!


Solution

  • You aren't reading the secrets file. Change your omniauth.rb to this:

    Rails.application.config.middleware.use OmniAuth::Builder do
      provider :facebook, Rails.application.secrets.FACEBOOK_APP_ID, Rails.application.secrets.FACEBOOK_SECRET,
               :scope => 'email,user_location'
    end
    

    PS: its necessary to restart the server after the changes