我在我的rails应用程序中使用Facebook Oauth和Devise。我成功地进入了facebook登录页面,但随后在回调操作中出错。在我的回调操作中将其跟踪到request.env["omniauth.auth"]
返回nil
。
的Gemfile:
gem 'devise'
gem 'omniauth'
gem 'omniauth-facebook'
路线:
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" }
初始化/ 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
我想知道request.env["omniauth.auth"]
为什么要返回nil
?
答案 0 :(得分:3)
所以最后我能找到问题,我也在很长一段时间内遇到同样的问题,但现在我们有了解决方案,好的,我们走了:
问题在于devise.rb,只需从文件中删除它:
config.omniauth :facebook, Figaro.env.facebook_key, Figaro.env.facebook_secret,
scope: 'email,public_profile',
callback_url: Figaro.env.facebook_callback_url
然后重新启动服务器,然后尝试通过Facebook登录。
你会得到你想要的东西:)
谢谢,享受编码:)