5

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?

Simpleton
  • 6,285
  • 11
  • 53
  • 87

2 Answers2

10

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.

Mikhail Nikalyukin
  • 11,867
  • 1
  • 46
  • 70
10

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.

ndemoreau
  • 3,849
  • 4
  • 43
  • 55
  • Yes, but i dont like this solution, it just doesnt `clean` enough for me. – Mikhail Nikalyukin Jan 27 '12 at 22:04
  • 3
    This is more secure than having your keys in a text file that you upload to the server. – ian Jun 25 '12 at 16:18
  • 3
    This is a nice solution so your passwords aren't stored in version control. [Heroku](https://devcenter.heroku.com/articles/config-vars) makes this easy too: `heroku config:add FACEBOOK_KEY=abc123xyz` – Andrew Oct 19 '12 at 19:59