我花了最后一天试图解决此问题,这让我发疯了。
昨晚,我在自己的网站上进行了Facebook登录,并正在检索基本用户信息。我的问题开始于我添加:scope => 'user_birthday'
到的内容config/initializers/omniauth.rb
,现在看起来像这样
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, "APP_ID", "APP_SECRET", :scope => 'user_birthday'
end
供参考,我config.omniauth :facebook, "APP_ID", "APP_SECRET"
从config/initializers/devise.rb
我花了几个小时试图使它起作用,但最终不得不放弃。然后今天早上我再次运行它,它奏效了。喜出望外,我试图添加另一个参数,:scope
但现在整个事情又坏了。如果删除,我可以使它工作,:scope
但是每次放回它时都会失败(即使:scope => 'user_birthday'
就像今天早上我在做第一件事一样)。
为了找到问题所在,我放入了调试代码,omniauth_callbacks_controller.rb
现在看起来像这样:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])
puts "start before persist debug"
puts @user.birthday
puts @user.persisted?
puts "end before persist debug"
if @user.persisted?
puts "Start persisted debug"
puts request.env["omniauth.auth"]
puts "End debug"
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
puts "Start unpersisted debug"
puts request.env["omniauth.auth"]
puts "End debug"
redirect_to new_user_registration_url
end
end
end
此调试清楚地表明,facebook正在返回必要的信息,但应用程序由于.persisted?
返回false而失败,因此我被重定向到new_user_registration页面,该页面返回以下内容:
NoMethodError in Devise::Registrations#new
Showing /home/action/workspace/cloudapp/app/views/devise/shared/_links.html.erb where line #23 raised:
undefined method `omniauth_authorize_path' for #<#<Class:0x007f3aeffff038>:0x007f3aefffdf08>
我无法为自己的生活弄清楚为什么.persisted?
返回假。我正在使用Nitrous.io通过Heroku PostgreSQL数据库进行开发。通过运行,我已经确认数据库中没有用户
rails c
User.all
返回:
User Load (89.4ms) SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation []>
我感觉问题出在models/user.rb
哪里,但是我无法弄清楚如何调试它以查看它是否在寻找用户,因此没有持久存在或试图创建一个失败的用户。有谁知道调试此问题的简单方法?
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
user.birthday = auth.extra.raw_info.birthday
# user.image = auth.info.image # assuming the user model has an image
end
end
我已经处理了大约50遍,几乎要放弃了。
我唯一能想到的就是where(provider: auth.provider, uid: auth.uid)
返回某些内容(因为我的数据库为空,所以不应该这样做)。数据库外部某处可能存在索引,这就是它要搜索的内容吗?
请出于我的理智,有人可以帮忙吗?如果您需要更多信息,我会很乐意提供
编辑1
尝试了以下方法,它的工作原理使我比以往更加困惑:
:scope => 'user_birthday'
左侧的Facebook登录。Facebook列出的权限为your public profile and birthday
。接受并发送回我的网站,该操作如上所述失败(即使信息肯定会发送回):scope => 'user_birthday'
并尝试再次使用Facebook登录。定向至Facebook,其中列出了寻求的许可your public profile and email address
。接受并定向回现在可以正常工作的站点,并且可以存储和访问用户生日,因为我从上面第2条得到了来自Facebook的许可。我现在完全茫然了
找出为什么不保存该对象。您需要放置错误。
puts @user.errors.to_a
并检查内容 auth
puts request.env["omniauth.auth"]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句