Devise: Omniauth callbacks is not working.

Created on 29 Mar 2017  路  7Comments  路  Source: heartcombo/devise

Hi all,

When am trying to sign up with facebook using omniauth_callbacks, am getting the following issues Authentication failure! invalid_credentials: OAuth2::Error, :
{"access_token":"","token_type":"bearer","expires_in":5169153},
but previously it is working fine for me. Please me from this situation.

My routes:-

devise_for :user ,:controllers => { sessions: 'sessions', :registrations => "registrations", passwords: 'passwords', omniauth_callbacks: "omniauth_callbacks" }

Devise,

config.omniauth :facebook, '1405366256194493', '12416db28ae9dc0a43ca92f21c4edd31', :scope => 'email,public_profile,user_hometown',:info_fields=>'id,email,gender,link,locale,first_name,last_name,timezone,updated_time,verified'

Omniauth controller,

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
   def facebook
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    auth = request.env["omniauth.auth"]
    @user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.from_omniauth(auth,request.env['omniauth.params'])
   if @user.persisted?
      if @user.role_id.nil?
          sign_in(@user, :bypass => true)
         redirect_to select_role_path
      else
        sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
        set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
      end
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

User model,

def self.from_omniauth(auth, role)
    if user = User.where(email: auth['info']['email']).first
      user
    else
      where(provider: auth.provider, uid: auth.uid).first_or_create do |u|
        u.email = auth.info.email
        u.password = Devise.friendly_token[0, 20]
        u.first_name = auth.info.first_name
        u.last_name = auth.info.last_name # assuming the user model has a name
        # u.profile_photo = auth.info.image
        u.role_id = role['role_id']
        u.skip_confirmation!
      end
    end
  end

Most helpful comment

I fixed this issue by updating the versions of ominauth-facebook. Now using version 4.0.0. Was using 2.2.0 >.<

All 7 comments

Also getting. Facebook updated their API behavior - killing v2.2.

I fixed this issue by updating the versions of ominauth-facebook. Now using version 4.0.0. Was using 2.2.0 >.<

Thanks man, you saved my saturday ;)

anyone facing issues with omniauth-google-oauth2 after this update?

For me also it is working fine, once i updated my ominauth-facebook version.

omniauth-google-oauth2 was working fine too, was something else that caused the issue.

gem 'omniauth-facebook', '~> 4.0.0' and fixing all the dependencies fixes the issue for me

Was this page helpful?
0 / 5 - 0 ratings