Guy Schaller
Guy Schaller

Reputation: 4700

rails environment variables from environment.rb in omniauth initializer

I am unable to read the variables I defined in environment.rb file for omniauth.

This is my code:

environment.rb

ENV['LINKEDIN_KEY'] = "key"
ENV['LINKEDIN_SECRET'] = "secret"

omniauth.rb initializer

OmniAuth.config.logger = Rails.logger

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :linkedin, ENV['linkedin_key'], ENV['linkedin_secret']
end

This only works for me if I hard code the key and secret. I am using Rails 4 and Ruby 2.

In the main example of omniauth they used it the same way:

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

I would love some help. Thanks.

Upvotes: 1

Views: 2035

Answers (2)

Guy Schaller
Guy Schaller

Reputation: 4700

After searching for the new dynamic-providers strategy omniauth suggest using I found this page: https://github.com/intridea/omniauth/wiki/Setup-Phase

so changed my code to:

SETUP_PROC = lambda do |env|
  env['omniauth.strategy'].options[:consumer_key] = ENV['LINKEDIN_KEY']
  env['omniauth.strategy'].options[:consumer_secret] = ENV['LINKEDIN_SECRET']
end

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :linkedin, setup: SETUP_PROC
end

And it works!

Upvotes: 2

Toby Hede
Toby Hede

Reputation: 37133

It is possible that the case is an issue in your middleware:

provider :linkedin, ENV['linkedin_key'], ENV['linkedin_secret']

In your environment.rb you are using upper case:

ENV['LINKEDIN_KEY'] = "key"
ENV['LINKEDIN_SECRET'] = "secret"

I also recommend you have a look at dotenv to manage you environment data - it keeps sensitive information from your source control.

Upvotes: 3

Related Questions