Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle OmniAuth callbacks in multiple environments?

I have an app that uses Facebook exclusively as a provider for authentication and have correctly setup the callback for production mode. In order for this to work, you provide your Facebook app with a site URL and a site domain for callbacks, and in my case it's http://appname.heroku.com and appname.heroku.com respectively.

The problem is that my controllers are setup to only allow authenticated sessions so I cannot view my app in development mode because the Facebook app's domain obviously hasn't been set to localhost.

How do I get around this without having to change it in Facebook's settings?

like image 568
Simpleton Avatar asked Dec 05 '11 13:12

Simpleton


2 Answers

Create another one facebook app with domain localhost:3000 for development and create config.yml in the config directory

development:
  facebook_api_key: 656756786867868
  facebook_api_secret: 4sdffdh6u345436

production:
  facebook_api_key: 45778799
  facebook_api_secret: fghjkbbcbcbcbcb

Now add load_config.rb to the initializers folder

# load config
AppConfig = YAML.load_file(Rails.root.join('config', 'config.yml'))

# Override config options by correct environment
env_options = AppConfig.delete(Rails.env)

AppConfig.merge!(env_options) unless env_options.nil?

And finally add this to the omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
   provider :facebook, AppConfig['facebook_api_key'], AppConfig['facebook_api_secret']           
end

It will take your keys depending on rails environment. That's all, hope it helps you.

like image 139
Mikhail Nikalyukin Avatar answered Oct 19 '22 22:10

Mikhail Nikalyukin


Why not just create ENV[] variables in the environments files and using them like this in your initializer:

provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK']
provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']

It seems much easier (and shorter) to me.

like image 42
ndemoreau Avatar answered Oct 19 '22 23:10

ndemoreau