53

I've been trying to figure out how Ryan Bates, in his Facebook Authentication screencast, is setting the following "FACEBOOK_APP_ID" and "FACEBOOK_SECRET" environment variables.

provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET']

There are similar-ish questions around, but no answers that I've been able to get to work on Rails 3.2.1.

UPDATE:

As of May 2013, my preferred way to handle ENV variables is via the Figaro gem

neon
  • 2,811
  • 6
  • 30
  • 44

3 Answers3

119

You could take a look at the comments:

You can either set environment variables directly on the shell where you are starting your server:

FACEBOOK_APP_ID=12345 FACEBOOK_SECRET=abcdef rails server

Or (rather hacky), you could set them in your config/environments/development.rb:

ENV['FACEBOOK_APP_ID'] = "12345";
ENV['FACEBOOK_SECRET'] = "abcdef";

An alternative way

However I would do neither. I would create a config file (say config/facebook.yml) which holds the corresponding values for every environment. And then load this as a constant in an initializer:

config/facebook.yml

development:
  app_id: 12345
  secret: abcdef

test:
  app_id: 12345
  secret: abcdef

production:
  app_id: 23456
  secret: bcdefg

config/initializers/facebook.rb

FACEBOOK_CONFIG = YAML.load_file("#{::Rails.root}/config/facebook.yml")[::Rails.env]

Then replace ENV['FACEBOOK_APP_ID'] in your code by FACEBOOK_CONFIG['app_id'] and ENV['FACEBOOK_SECRET'] by FACEBOOK_CONFIG['secret'].

Peter Brown
  • 50,956
  • 18
  • 113
  • 146
iblue
  • 29,609
  • 19
  • 89
  • 128
  • 3
    awesome - works for me. and then place facebook.yml in .gitignore - correct? also, one caveat - if using the variable in another initializer (as I am with devise.rb), make sure you rename your facebook.rb to come alphabetically before the file you need the variables in. In my case, I had to rename facebook.rb to config_facebook.rb in order for it to be loaded before devise.rb – neon Jul 25 '12 at 11:56
  • 7
    just be aware that the order of that the initializers load is alphabetical. So if you need the config in another inititializer you will have to name the facebook.rb something like 01_facebook.rb (not too pretty - but works) – Dimitris Nov 22 '12 at 08:34
  • 1
    Any advice about whether to put the YAML file/Foreman .env into Git or to ignore it? I'd like to deploy to Heroku, but I'd also like to be able to publish my code on Github, so for one I'd need it checked in, the other I'd need it ignored. – dsample Feb 03 '13 at 18:17
  • Maybe create two separate branches, push one to github and the other one to Heroku? – iblue Feb 03 '13 at 21:35
  • 2
    Nitpicking: shouldn't `config/initializer/facebook.rb` be `config/initializers/facebook.rb` with an 's'? – GMA Jun 22 '13 at 10:18
29

There are several options:

  • Set the environment variables from the command line:

    export FACEBOOK_APP_ID=your_app_id
    export FACEBOOK_SECRET=your_secret
    

    You can put the above lines in your ~/.bashrc

  • Set the environment variables when running rails s:

    FACEBOOK_APP_ID=your_app_id FACEBOOK_SECRET=your_secret rails s
    
  • Create a .env file with:

    FACEBOOK_APP_ID=your_app_id
    FACEBOOK_SECRET=your_secret
    

    and use either Foreman (starting your app with foreman start) or the dotenv gem.

Stefan
  • 109,145
  • 14
  • 143
  • 218
  • Foreman is the solution recommended by Heroku (https://devcenter.heroku.com/articles/config-vars), that has commands to set them for the production env. Note that you should not commit the .env file on your Version Control. Consider a README explaining how to set it up. – ejoubaud Apr 01 '13 at 13:42
12

Here's another idea. Define the keys and values in provider.yml file, as suggested above. Then put this in your environment.rb (before the call to Application.initialize!):

YAML.load_file("#{::Rails.root}/config/provider.yml")[::Rails.env].each {|k,v| ENV[k] = v }

Then these environment variables can be referenced in the omniauth initializer without any ordering dependency among intializers.

Jan Hettich
  • 9,146
  • 4
  • 35
  • 34