在Devise中使用电子邮件+密码查找用户

时间:2012-01-17 15:55:11

标签: ruby-on-rails passwords devise

My Rails应用使用旧数据库中的数据。此数据库中的导入用户包含重复的电子邮件。使用电子邮件+密码进行身份验证(这是数据库中的唯一组合)。 设计使用方法find_for_database_authentication来查找用户。但是params不包含密码(只是登录名)。

我该怎么办?

2 个答案:

答案 0 :(得分:2)

试试这个:

find_user = User.where("email = ?", params["user"]["email"]).first
find_user.valid_password?(params["user"]["password"])

答案 1 :(得分:1)

你可以这样搜索

用户模型中的

覆盖了find方法:

def self.find_for_database_authentication(warden_conditions)
   conditions = warden_conditions.dup

   email = conditions.delete(:email)
   pwd = conditions.delete(:password)
   encrypted_pwd = User.new(password: pwd).encrypted_password

   where(conditions).where(["lower(email) = :email AND encrypted_password = :pwd", { :email => email.strip.downcase, :pwd => encrypted_pwd }]).first
end

可能config / initializers / devise.rb需要像:

 config.authentication_keys = [ :email, :password ]