2

My facebook app setup only works on my localhost, but not the heroku site.

I get this error on heroku logs.

    ERROR -- omniauth: (facebook) Authentication failure! no_authorization_code: OmniAuth::Strategies::Facebook::NoAuthorizationCodeError, must pass either a `code` (via URL or by an `fbsr_XXX` signed request cookie)

On facebook settings/advanced, This is the setup I have: Valid OAuth redirect URIs is = http://localhost:3000

On facebook settings/basic, my App Domains is = localhost

and my Site URL is = http://localhost:3000/

my devise.rb

    config.omniauth :facebook, 'somekey', 'somekey', scope: 'email', info_fields: 'email, name'

my omniauth_callbacks_controller.rb

    class OmniauthCallbacksController < Devise::OmniauthCallbacksController 

    def facebook
@user = User.from_omniauth(request.env["omniauth.auth"])

if @user.persisted?
  sign_in_and_redirect @user, :event => :authentication
  set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
  session["devise.facebook_data"] = request.env["omniauth.auth"]
  redirect_to new_user_registration_url
end
  end
  end

My app/models/user.rb

    class User < ActiveRecord::Base
    devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable

    def self.from_omniauth(auth)
result = User.where(email: auth.info.email).first

if result
  return result
else
  where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
    user.fullname = auth.info.name
    user.provider = auth.provider
    user.uid = auth.uid
    user.email = auth.info.email
    user.image = auth.info.image
    user.password = Devise.friendly_token[0, 20]
  end
end
 end
 end

In my app/views/devise/sessions/new.html.erb,

<%= link_to "Sign In with Facebook", user_omniauth_authorize_path(:facebook) %>
Anna_Natorilla
  • 157
  • 2
  • 13
  • Do you have a config/initializers/omniauth.rb initializer? Can you edit your question with this file? My guess is you have not set ENV['FACEBOOK_KEY'] and ENV['FACEBOOK_SECRET'] in your heroku config? – rlarcombe Nov 17 '15 at 12:11
  • I placed the facebook secret keys at config/initializers/devise.rb. How do edit the question? In https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview, It said config/initializers/omniauth.rb will clash with config.omniauth which what I tried doing. – Anna_Natorilla Nov 17 '15 at 12:33
  • To edit your question above, just click the small gray "edit" link. Then add your initializer file. This will help debug it for you. – rlarcombe Nov 17 '15 at 13:16

1 Answers1

1

In your devise.rb, I recommend using ENV variables like so:

config.omniauth :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], scope: 'email', info_fields: 'email, name'

In development mode, you can use the very helpful Dotenv gem to configure these locally.

Then set these in the Heroku config with:

heroku config:set FACEBOOK_KEY="your_fb_app_key"
heroku config:set FACEBOOK_SECRET="your_fb_app_secret"

Once this is done, your Heroku app should pick up the right Facebook Credentials. Just make sure that your Facebook App is configured to work with your production Heroku URLS in the App Domains settings.

rlarcombe
  • 2,958
  • 1
  • 17
  • 22