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) %>
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.