【问题标题】:Improve logic for method改进方法的逻辑
【发布时间】:2015-07-20 19:02:53
【问题描述】:

我有这个方法,我将通过该方法检查提供者和 uid 属性,以在创建新寄存器或获取现有寄存器之间进行选择。
这是将 facebook 登录集成到我的应用程序中。 如果我想检查已经在我的应用程序中注册的用户,而不是通过 facebook,使用他们的电子邮件地址作为搜索键,然后将来自 facebook 的 proivider 和 uid 属性添加到该帐户,那么一个好的逻辑是什么?

def self.from_omniauth(hash) 
    where(provider: hash.provider, uid: hash.uid).first_or_create do |user|
      user.email = hash.info.email
      user.password = Devise.friendly_token[0,20]
      user.username = hash.info.name
end

更新

类用户::OmniauthCallbacksController

 def facebook

    @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?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

我按照你建议的方法更新了代码

def self.from_omniauth(hash)
    find_by_provider_and_uid(hash.provider, hash.uid) || find_by_omni_email(hash.info.email) || create_with_omniauth(hash)
  end

  def self.create_with_omniauth(hash)
    u = User.new
    u.provider = hash.provider
    u.uid = hash.uid
    u.email = hash.info.email
    u.password = Devise.friendly_token[0,20]
    u.username = hash.info.name

    puts "tried to create_with+omniauth"
  end

  def self.find_by_omni_email(email)
    User.where(email: email).first

    puts "ran find_by_omni_email"


  end

  def self.find_by_provider_and_uid(provider, uid)
    User.where(provider: provider, uid: uid).first

    puts "ran find_by_provider_and_uid"
  end

【问题讨论】:

  • 这一行:where(provider: hash.provider, uid: hash.uid).first_or_create 如果用户已经存在,则不会创建任何新记录。

标签: ruby-on-rails oauth


【解决方案1】:

以这种方式改变你的逻辑。它将检查所有场景

def self.from_omniauth(hash)
  find_by_provider_and_uid(hash.provider, hash.uid) || find_by_omni_email(hash.info.email) || create_with_omniauth(hash)
end

def self.create_with_omniauth(hash)
  u = User.new
  u.provider = hash.provider
  u.uid = hash.uid
  u.email = hash.info.email
  u.password = Devise.friendly_token[0,20]
  u.username = hash.info.name
  u.save
  u
end

def self.find_by_omni_email(email)
  User.where(email: email).first
end

#此方法在活动记录下方默认提供。如果没有或有一些变化,我会添加以防万一

def self.find_by_provider_and_uid(provider, uid)
  User.where(provider: provider, uid: uid).first
end

【讨论】:

  • 我试图了解这段代码是如何工作的。说 find_by_provider_and_uid 没有找到寄存器,实际上从 User.where(provider: provider, uid: uid).first 返回了什么?它是一个布尔值(假?),然后使 find_by_omni_email 运行?在这种情况下,如果找到一个帐户,我不需要将提供程序和 uid 值添加到该帐户的代码吗?
  • 首先它会通过提供者和uid找到用户。如果找到。它将作为用户返回。如果不是,它将尝试通过电子邮件查找,如果通过电子邮件找到该用户将被返回。当find_by_provider_and_uidfind_by_omni_email 都没有返回任何内容时,它将调用create_with_omniauth 并创建一个用户并返回。
  • 如果 User.where(provider: provider, uid: uid).first 没有找到用户?它会返回 nil
  • 我试过你的代码,它似乎工作正常,但我遇到了这个问题
  • 未定义的方法“持续存在?”对于 nil:NilClass ---------------------------------------------- ------------------------------------- def facebook @user = User.from_omniauth(request.env[ "omniauth.auth"]) 如果@user.persisted 则放入"puts" + @user.to_s? sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not激活 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
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多