Rails, Devise, and Facebook Oauth: request.env ["omniauth.auth"] nil

I use Facebook Oauth and Devise in my rails application. I successfully get to the facebook login page, but then I get an error message. Traced it to request.env["omniauth.auth"] , returning nil to my callback action.

Gemfile:

 gem 'devise' gem 'omniauth' gem 'omniauth-facebook' 

Routes

  devise_scope :user do get '/users/auth/facebook/callback', to: 'users/omniauth_callbacks#facebook' end resources :users devise_for :users, path: '', path_names: { sign_up: 'register', sign_in: 'login', sign_out: 'logout'}, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } 

The initializers / devise.rb:

 config.omniauth :facebook, Figaro.env.facebook_key, Figaro.env.facebook_secret, scope: 'email,public_profile', callback_url: Figaro.env.facebook_callback_url 

User.rb:

 class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :confirmable, :validatable, :omniauthable, :omniauth_providers => [:facebook] 

omniauth_callbacks_controller.rb:

 class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController def facebook @user = User.from_omniauth(request.env["omniauth.auth"]) request.env["omniauth.auth"] ## <<=== this is nil 

Any idea why my request.env["omniauth.auth"] returns nil ?

+6
source share
1 answer

So finally, I can find the problem, I also struggled with the same problem for a long time, but now we have a solution, well, here we go:

The problem with devise.rb, just delete this from the file:

 config.omniauth :facebook, Figaro.env.facebook_key, Figaro.env.facebook_secret, scope: 'email,public_profile', callback_url: Figaro.env.facebook_callback_url 

and then reboot the server, and then try logging in via facebook.

You will get what you want :)

Thanks, Enjoy the coding :)

+2
source

Source: https://habr.com/ru/post/1014395/


All Articles