'omniauth: (facebook) Authentication failure! uninitialized constant Users: NameError, uninitialized constant Users
I have been looking for the solution but couldn't got luck, tried multiple solutions too.. but yet stuck..
What I am trying to do is adding facebook login to my app.. I'll try to explain and share the code as much as possible.
ruby version: 2.6.6
rails: 6.0.3
Here is the Gemfile
gem 'omniauth', '~> 2.0', '>= 2.0.4'
gem 'omniauth-oauth2', '~> 1.7', '>= 1.7.2'
gem 'omniauth-facebook'
gem 'devise', '~> 4.8', '>= 4.8.1'
gem 'simple_token_authentication', '1.17.0'
gem 'devise-async', '1.0.0'
gem 'activeadmin', '~> 2.6', '>= 2.6.1'
the devise.rb file
config.omniauth :facebook, "APPID", "APPSECRET", callback_url: "http://localhost:3000/users/auth/facebook/callback"
here is my User Model
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:trackable, :async,
:omniauthable, :omniauth_providers => [:facebook]
## Token Authenticatable
acts_as_token_authenticatable
def devise_mailer
UserMailer
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
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.encrypted_password = Devise.friendly_token[0,20]
user.first_name = auth.info.name # assuming the user model has a name
end
end
end
in routes file
devise_for :users, controllers: { registrations: "api/v1/users/registrations", sessions: "api/v1/users/sessions", passwords: "api/v1/users/passwords", omniauth_callbacks: "users/omniauth_callbacks" }
the omniauth controller app/controller/api/v1/users/omniauth_callbacks_controller.rb
# frozen_string_literal: true
class Api::V1::Users::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
def failure
redirect_to root_path
end
end
and finally the link for social login link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path, method: :post which is generating the URL http://localhost:3000/api/v1/users/auth/facebook
and when clicked, I get below error
Started POST "/api/v1/users/auth/facebook" for 172.19.0.1 at 2022-02-15 12:47:28 +0000
D, [2022-02-15T12:47:28.580549 #1] DEBUG -- omniauth: (facebook) Request phase initiated.
W, [2022-02-15T12:47:28.582705 #1] WARN -- omniauth: Attack prevented by OmniAuth::AuthenticityTokenProtection
E, [2022-02-15T12:47:28.582960 #1] ERROR -- omniauth: (facebook) Authentication failure! authenticity_error: OmniAuth::AuthenticityError, Forbidden
E, [2022-02-15T12:47:28.591291 #1] ERROR -- omniauth: (facebook) Authentication failure! uninitialized constant Users: NameError, uninitialized constant Users
NameError (uninitialized constant Users):
activesupport (6.0.4.6) lib/active_support/inflector/methods.rb:282:in `const_get'
activesupport (6.0.4.6) lib/active_support/inflector/methods.rb:282:in `block in constantize'
activesupport (6.0.4.6) lib/active_support/inflector/methods.rb:280:in `each'
activesupport (6.0.4.6) lib/active_support/inflector/methods.rb:280:in `inject'
activesupport (6.0.4.6) lib/active_support/inflector/methods.rb:280:in `constantize'
devise (4.8.1) lib/devise/omniauth.rb:18:in `block in <main>'
omniauth (2.0.4) lib/omniauth/strategy.rb:544:in `fail!'
omniauth (2.0.4) lib/omniauth/strategy.rb:199:in `rescue in call!'
omniauth (2.0.4) lib/omniauth/strategy.rb:191:in `call!'
omniauth (2.0.4) lib/omniauth/strategy.rb:169:in `call'
warden (1.2.9) lib/warden/manager.rb:36:in `block in call'
warden (1.2.9) lib/warden/manager.rb:34:in `catch'
warden (1.2.9) lib/warden/manager.rb:34:in `call'
rack (2.2.3) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.2.3) lib/rack/etag.rb:27:in `call'
rack (2.2.3) lib/rack/conditional_get.rb:40:in `call'
rack (2.2.3) lib/rack/head.rb:12:in `call'
I have gone through many solutions shared on stackoverflow and github#issues but none worked for me.. If anyone can tell me what I am doing wrong here that'll be really great...
I have tried to share my code and explain the problem... but if incase I missed something, then please accept my apologies, I'll be happy to share any further information if missed or asked..
Thank you in advance..
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
