简体   繁体   中英

Rails, Devise, and Facebook Oauth: request.env[“omniauth.auth”] is nil

I'm using Facebook Oauth and Devise in my rails app. I successfully get taken to the facebook login page, but then get an error on the callback action. Tracked it down to request.env["omniauth.auth"] returning nil in 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" }

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"] is returning nil ?

So finally i am able to find the issue, i was also struggling with the same issue for a long time, but now we have the solution, ok here we go:

The problem is with the devise.rb, just remove 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 restart your server and then try to get login through facebook.

You will get what you want :)

Thanks, Enjoy Coding :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM