簡體   English   中英

在Devise中使用電子郵件+密碼查找用戶

[英]Find user using email + password in Devise

我的Rails應用程序使用舊數據庫中的數據。 從此數據庫導入的用戶包含重復的電子郵件。 使用電子郵件和密碼進行身份驗證(這是數據庫中的唯一組合)。 Devise使用方法find_for_database_authentication查找用戶。 但是,參數不包含密碼(僅登錄名)。

我能做什么?

嘗試這個 :

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

你可以這樣搜索

在用戶模型中覆蓋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將需要smth,如:

 config.authentication_keys = [ :email, :password ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM