繁体   English   中英

将omniauth-twitter gem与devise和rails一起使用5

[英]Using the omniauth-twitter gem with devise and rails 5

我开始看Ryan Bates的关于如何将Omniauth与devise一起使用的教程 (修订了rails-cast 235)。 我在使用user.rb文件时遇到问题。 他在教程中显示的内容

 devise :omniauthable, # ... def self.from_omniauth(auth) where(auth.slice(:provider, :uid)).first_or_create do |user| user.provider = auth.provider user.uid = auth.uid user.username = auth.info.nickname end end def self.new_with_session(params, session) if session["devise.user_attributes"] new(session["devise.user_attributes"], without_protection: true) do |user| user.attributes = params user.valid? end else super end end def password_required? super && provider.blank? end def update_with_password(params, *options) if encrypted_password.blank? update_attributes(params, *options) else super end end 

对我不起作用,它显示ActiveModel :: ForbiddenAttributesError和以下代码行where(auth.slice(:provider, :uid)).first_or_create do |user| 突出显示。 我认为他的版本不起作用,因为我正在使用Rails5。无论如何,我都尝试过修改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.provider = auth.provider user.uid = auth.uid token = auth.credentials.token, secret = auth.credentials.secret end end def self.new_with_session(params, session) super.tap do |user| if data = session["devise.twitter_data"] # user.attributes = params user.update( email: params[:email], password: Devise.friendly_token[0,20], provider: data["provider"], uid: data["token"], token: data["credentials"]["token"], secret: data["credentials"]["secret"] ) end end end def password_required? super && provider.blank? end def update_with_password(params, *options) if encrypted_password.blank? update_attributes(params, *options) else super end end end 

我能够获取twitter身份验证页面以显示负载大约一分钟,然后重定向回我的用户注册页面,而无需登录,也不会显示任何错误消息。 顺便说一句,没有人知道如何使用devise与Facebook登录。

问题与传递给self.from_omniauth(auth)的属性auth有关。 您应该设置binding.pryputs auth放在服务器日志中,以便查看此变量的外观。

可能当self.from_amniauth(auth)方法运行此auth.slice()它以某种方式运行时出错

同样, first_or_create方法可能正在创建用户,并且您可能缺少某些内容,从而引发错误。 因此,尝试通过debug show使用user.errors.full_messages

听到的想法是该方法旨在查找或创建User 它是从控制器操作Users::OmniauthCallbacksController#facebook

request表示这是从您的服务器到facebooktwitter的传出request

Facebook或Twitter将通过AJAX response来回复此request ,其中将包含您需要验证用户身份的信息。

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"])

    if @user.persisted?
      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?, you probably saved the env variable
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

  def failure
    redirect_to root_path
  end, you probably saved the env variable
end

然后使用以下方法where(provider: auth.provider, uid: auth.uid).first_or_create您将创建user或从数据库中find它。 问题是您可能将此方法写错或用错误的参数调用它,或者您未使用Twitter设置api才能正常工作...

来自https://github.com/plataformatec/devise/wiki/OmniAuth:-概述

此操作有几个方面值得描述:

OmniAuth从Facebook检索到的所有信息都可以通过request.env [“ omniauth.auth”]的哈希值获得。 检查OmniAuth文档和每个omniauth-facebook gem的自述文件,以了解将返回哪些信息。

找到有效用户后,可以使用以下两种Devise方法之一登录他们:sign_in或sign_in_and_redirect。 传递:event =>:authentication是可选的。 仅当您希望使用Warden回调时,才应该这样做。

也可以使用Devise的默认消息之一来设置即显消息,但这取决于您。

万一用户不持久,我们将OmniAuth数据存储在会话中。 注意,我们使用“ devise”存储此数据。 作为键名称空间。 这很有用,因为Devise会删除所有以“ devise”开头的数据。 每当用户登录时都会从会话中获取信息,因此我们会自动清除会话。 最后,我们将用户重定向回我们的注册表单。

定义控制器后,我们需要在模型中实现from_omniauth方法(例如,app / 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.image = auth.info.image # assuming the user model has an image
    # If you are using confirmable and the provider(s) you use validate emails, 
    # uncomment the line below to skip the confirmation emails.
    # user.skip_confirmation!
  end
end

此方法尝试通过provider和uid字段查找现有用户。 如果找不到用户,则会使用随机密码和一些其他信息创建一个新用户。 请注意,在创建新用户时,first_or_create方法会自动设置provider和uid字段。 first_or_create! 方法的操作类似,不同之处在于,如果用户记录验证失败,它将引发Exception。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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