I am working on devise omniauth to authenticate user using facebook. I have implemented it using the documentation https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview It is not working fine. When I login using my facebook account then it takes me to the "failure" method in the callback controller. But the interesting thing is that when I open facebook it open without any problem means it is authenticating my account. I have checked it so many times by login and logout from facebook. But the problem is that If it is authenticating facebook account then why it takes me to "failure method". here is my code
callback controller.
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
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?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def failure
render plain: params.inspect
# redirect_to root_path
end
end
User model
class User < ApplicationRecord
# Include default devise modules. Others available are:
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
devise :omniauthable, :omniauth_providers => [:facebook]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
# If you are using confirmable and the provider(s) you use validate emails,
# uncomment the line below to skip the confirmation emails.
# user.skip_confirmation!
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
end
route code
devise_for :users, controllers: {confirmations: 'confirmations',registrations: 'users/registrations',omniauth_callbacks: 'users/omniauth_callbacks' } do
#put "confirm_user", to: "confirmations#confirm_user"
get "confirmation", to: "confirmations#after_confirmation_path_for"
end
Facebook app setting screenshot is
https://www.dropbox.com/s/vn8cjpr5wyagkdp/Screenshot%202017-11-27%2017.04.28.png?dl=0
development logs after sign in are
Started GET "/users/auth/facebook" for 10.0.2.2 at 2017-11-28 14:13:37 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Started GET "/users/auth/facebook/callback?code=AQA8oLlPsMLSvZf5NqZfOA0oJxDVKczwqqMIxWPD2dJoucpJl9T7MLTmf0mMDlOhgQPAhKa2f6My4vWGZmTWhVo6S5TbSQ3ELup1iBhDHTb869pMMo-ksa1Kh1gJDG65ZJxUj5vAe8jH-DX0eBRpf_ygZACCoFYFTiZdAIzCXQI7jfLaeqH70CqAffkGZczYzhjThM_NLol3Lzo18ZX_6_5n2-p7nMC3IKhmzDEyo_toyaI1telD3QMwa0re7GIu-UXKV4DQp-ClLT452Bigp9Fhs50wYm-Kl08E7195R2mpBESpB7Gu0moDbCgi61dEEk5u8GGfmm0Cxbu9Fcw1_Eu8" for 10.0.2.2 at 2017-11-28 14:13:40 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Users::OmniauthCallbacksController#failure as HTML
Parameters: {"code"=>"AQA8oLlPsMLSvZf5NqZfOA0oJxDVKczwqqMIxWPD2dJoucpJl9T7MLTmf0mMDlOhgQPAhKa2f6My4vWGZmTWhVo6S5TbSQ3ELup1iBhDHTb869pMMo-ksa1Kh1gJDG65ZJxUj5vAe8jH-DX0eBRpf_ygZACCoFYFTiZdAIzCXQI7jfLaeqH70CqAffkGZczYzhjThM_NLol3Lzo18ZX_6_5n2-p7nMC3IKhmzDEyo_toyaI1telD3QMwa0re7GIu-UXKV4DQp-ClLT452Bigp9Fhs50wYm-Kl08E7195R2mpBESpB7Gu0moDbCgi61dEEk5u8GGfmm0Cxbu9Fcw1_Eu8"}
Rendering text template
Rendered text template (0.0ms)
Completed 200 OK in 20ms (Views: 10.6ms | ActiveRecord: 0.0ms)
The result of params.inspect is as follows
<ActionController::Parameters {"code"=>"AQA8oLlPsMLSvZf5NqZfOA0oJxDVKczwqqMIxWPD2dJoucpJl9T7MLTmf0mMDlOhgQPAhKa2f6My4vWGZmTWhVo6S5TbSQ3ELup1iBhDHTb869pMMo-ksa1Kh1gJDG65ZJxUj5vAe8jH-DX0eBRpf_ygZACCoFYFTiZdAIzCXQI7jfLaeqH70CqAffkGZczYzhjThM_NLol3Lzo18ZX_6_5n2-p7nMC3IKhmzDEyo_toyaI1telD3QMwa0re7GIu-UXKV4DQp-ClLT452Bigp9Fhs50wYm-Kl08E7195R2mpBESpB7Gu0moDbCgi61dEEk5u8GGfmm0Cxbu9Fcw1_Eu8"} permitted: false>
The output of rake routes is
rake routes
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format) users/omniauth_callbacks#passthru
user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format) users/omniauth_callbacks#facebook
user_twitter_omniauth_authorize GET|POST /users/auth/twitter(.:format) users/omniauth_callbacks#passthru
user_twitter_omniauth_callback GET|POST /users/auth/twitter/callback(.:format) users/omniauth_callbacks#twitter
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
user_registration PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
POST /users(.:format) users/registrations#create
new_user_confirmation GET /users/confirmation/new(.:format) confirmations#new
user_confirmation GET /users/confirmation(.:format) confirmations#show
POST /users/confirmation(.:format) confirmations#create
new_model_session GET /models/sign_in(.:format) devise/sessions#new
model_session POST /models/sign_in(.:format) devise/sessions#create
destroy_model_session DELETE /models/sign_out(.:format) devise/sessions#destroy
new_model_password GET /models/password/new(.:format) devise/passwords#new
edit_model_password GET /models/password/edit(.:format) devise/passwords#edit
model_password PATCH /models/password(.:format) devise/passwords#update
PUT /models/password(.:format) devise/passwords#update
POST /models/password(.:format) devise/passwords#create
cancel_model_registration GET /models/cancel(.:format) devise/registrations#cancel
new_model_registration GET /models/sign_up(.:format) devise/registrations#new
edit_model_registration GET /models/edit(.:format) devise/registrations#edit
model_registration PATCH /models(.:format) devise/registrations#update
PUT /models(.:format) devise/registrations#update
DELETE /models(.:format) devise/registrations#destroy
POST /models(.:format) devise/registrations#create
new_model_confirmation GET /models/confirmation/new(.:format) devise/confirmations#new
model_confirmation GET /models/confirmation(.:format) devise/confirmations#show
POST /models/confirmation(.:format) devise/confirmations#create
donations_donor_history GET /donations/donor_history(.:format) donations#donor_history
donations_donor_signup GET /donations/donor_signup(.:format) donations#donor_signup
POST /donations/donor_signup(.:format) donations#donor_signup
donations_sms_service POST /donations/sms_service(.:format) donations#sms_service
donations_create_user_account POST /donations/create_user_account(.:format) donations#create_user_account
donations_add_user_payroll POST /donations/add_user_payroll(.:format) donations#add_user_payroll
donations GET /donations(.:format) donations#index
edit_donation GET /donations/:id/edit(.:format) donations#edit
donation GET /donations/:id(.:format) donations#show
PATCH /donations/:id(.:format) donations#update
PUT /donations/:id(.:format) donations#update
DELETE /donations/:id(.:format) donations#destroy
campaign_donations POST /campaigns/:campaign_id/donations(.:format) donations#create
new_campaign_donation GET /campaigns/:campaign_id/donations/new(.:format) donations#new
campaign_donations_create_user_account GET /campaigns/:campaign_id/donations/create_user_account(.:format) donations#create_user_account
campaigns GET /campaigns(.:format) campaigns#index
POST /campaigns(.:format) campaigns#create
new_campaign GET /campaigns/new(.:format) campaigns#new
edit_campaign GET /campaigns/:id/edit(.:format) campaigns#edit
campaign GET /campaigns/:id(.:format) campaigns#show
PATCH /campaigns/:id(.:format) campaigns#update
PUT /campaigns/:id(.:format) campaigns#update
DELETE /campaigns/:id(.:format) campaigns#destroy
organizations GET /organizations(.:format) organizations#index
POST /organizations(.:format) organizations#create
new_organization GET /organizations/new(.:format) organizations#new
edit_organization GET /organizations/:id/edit(.:format) organizations#edit
organization GET /organizations/:id(.:format) organizations#show
PATCH /organizations/:id(.:format) organizations#update
PUT /organizations/:id(.:format) organizations#update
DELETE /organizations/:id(.:format) organizations#destroy
admins_social_sharing_switch POST /admins/social_sharing_switch(.:format) admins#social_sharing_switch
admins_error_detail GET /admins/error_detail(.:format) admins#error_detail
GET /admins/generate_report/:id(.:format) admins#generate_report
admins_create_company GET /admins/create_company(.:format) admins#create_company
POST /admins/create_company(.:format) admins#create_company
admins_revenue_detail GET /admins/revenue_detail(.:format) admins#revenue_detail
admins_create_account GET /admins/create_account(.:format) admins#create_account
admins_view_account GET /admins/view_account(.:format) admins#view_account
GET /admins/view_company/:id(.:format) admins#view_company
admins_donation_analysis GET /admins/donation_analysis(.:format) admins#donation_analysis
admins_link_expiry GET /admins/link_expiry(.:format) admins#link_expiry
admins_edit_profile GET /admins/edit_profile(.:format) admins#edit_profile
admins_update_profile POST /admins/update_profile(.:format) admins#update_profile
POST /admins/create_account(.:format) admins#create_account
admin_destroy GET /admins/:id(.:format) admins#destroy
admins GET /admins(.:format) admins#index
POST /admins(.:format) admins#create
new_admin GET /admins/new(.:format) admins#new
edit_admin GET /admins/:id/edit(.:format) admins#edit
admin GET /admins/:id(.:format) admins#show
PATCH /admins/:id(.:format) admins#update
PUT /admins/:id(.:format) admins#update
DELETE /admins/:id(.:format) admins#destroy
crons_expirylink_alert GET /crons/expirylink_alert(.:format) crons#expirylink_alert
users_sign_out GET /users/sign_out(.:format) devise/sessions#destroy
root GET / campaigns#latest
After hardwork of 2 days I have finally solved it myself. Every thing was fine except the gem itself. If you just write the gem omniauth-facebook
it would install version 1.4.0 which is very old. I update it to gem 'omniauth-facebook', '~> 4.0'
and it is working like a charm. So if anyone face the same issue he should update the gem.