omniauth-facebook gem: request.env["omniauth.auth"] 为零
omniauth-facebook gem: request.env["omniauth.auth"] is nil
我正在使用 omniauth-facebook gem with devise。直到最近它还在工作。我最近也从 Rails 4 升级到 Rails 5.0.1,但我不确定这就是原因。
我目前有 0 个用户,并且我已登录 Facebook。但是,当我尝试在本地主机上使用 Facebook 注册我的应用程序时,出现此错误:
NoMethodError in RegistrationsController#facebook
undefined method `provider' for nil:NilClass
这是我的用户模型。我标记了错误突出显示的行。
User.rb
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user| #ERROR
@data = auth.info
user.name = @data.name
# ...
end
end
RegistrationsController
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
此外,这是我的 link:
<%= link_to "fb", user_facebook_omniauth_callback_path(:facebook, thing: @thing.id, degree: @degree, :format => :js) %>
HTML输出:
<a href=\"/auth/facebook/callback.js?thing=2\">fb<\/a>
路径:
localhost:3000/auth/facebook/callback.js?thing=2
所以问题是 request.env["omniauth.auth"]
由于某种原因为零。我在任何文档中都找不到任何类似错误的痕迹。
有人遇到过这个问题或有任何想法吗?
要通过 facebook 进行身份验证,您只需将 link 从您的站点发送到 facebook,如下所示:
www.yoursite.com/auth/facebook
然后设置一个路由以使用身份验证哈希接收来自 Facebook 的回调:
#routes.rb
get 'auth/facebook/callback' => 'sessions#create_facebook'
你能具体说明这行的输出是怎样的,或者你为什么要传递其他信息吗?:
<%= link_to "fb", user_facebook_omniauth_callback_path(:facebook, thing: @thing.id, degree: @degree, :format => :js) %>
编辑
auth/facebook/callback 是一个获取请求。 Facebook 向您发送用户身份验证哈希。只有 facebook 本身应该使用该路由。当你想验证你的 link 必须是:
localhost:3000/auth/facebook
按照您的方式,omniauth 需要 facebook 的身份验证哈希,但收到“?thing=2”,这会导致身份验证失败。 Omniauth 尝试从不是散列的“?thing=2”中提取信息,当您尝试访问 auth[provider] 时,auth 为空,因此未定义提供者,这会导致:
undefined method `provider' for nil:NilClass
我遇到了同样的问题,通过从用户模型
中删除 :omniauthable
解决了这个问题
我正在使用 omniauth-facebook gem with devise。直到最近它还在工作。我最近也从 Rails 4 升级到 Rails 5.0.1,但我不确定这就是原因。
我目前有 0 个用户,并且我已登录 Facebook。但是,当我尝试在本地主机上使用 Facebook 注册我的应用程序时,出现此错误:
NoMethodError in RegistrationsController#facebook
undefined method `provider' for nil:NilClass
这是我的用户模型。我标记了错误突出显示的行。
User.rb
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user| #ERROR
@data = auth.info
user.name = @data.name
# ...
end
end
RegistrationsController
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
此外,这是我的 link:
<%= link_to "fb", user_facebook_omniauth_callback_path(:facebook, thing: @thing.id, degree: @degree, :format => :js) %>
HTML输出:
<a href=\"/auth/facebook/callback.js?thing=2\">fb<\/a>
路径:
localhost:3000/auth/facebook/callback.js?thing=2
所以问题是 request.env["omniauth.auth"]
由于某种原因为零。我在任何文档中都找不到任何类似错误的痕迹。
有人遇到过这个问题或有任何想法吗?
要通过 facebook 进行身份验证,您只需将 link 从您的站点发送到 facebook,如下所示:
www.yoursite.com/auth/facebook
然后设置一个路由以使用身份验证哈希接收来自 Facebook 的回调:
#routes.rb
get 'auth/facebook/callback' => 'sessions#create_facebook'
你能具体说明这行的输出是怎样的,或者你为什么要传递其他信息吗?:
<%= link_to "fb", user_facebook_omniauth_callback_path(:facebook, thing: @thing.id, degree: @degree, :format => :js) %>
编辑
auth/facebook/callback 是一个获取请求。 Facebook 向您发送用户身份验证哈希。只有 facebook 本身应该使用该路由。当你想验证你的 link 必须是:
localhost:3000/auth/facebook
按照您的方式,omniauth 需要 facebook 的身份验证哈希,但收到“?thing=2”,这会导致身份验证失败。 Omniauth 尝试从不是散列的“?thing=2”中提取信息,当您尝试访问 auth[provider] 时,auth 为空,因此未定义提供者,这会导致:
undefined method `provider' for nil:NilClass
我遇到了同样的问题,通过从用户模型
中删除:omniauthable
解决了这个问题