2012-01-17 96 views
0

My Rails應用程序使用來自傳統數據庫的數據。來自此數據庫的導入用戶包含重複的電子郵件。使用電子郵件+密碼進行身份驗證(並且它是數據庫中的獨特組合)。 設計使用方法find_for_database_authentication找到用戶。然而,參數不包含密碼(只是登錄名)。在Devise中使用電子郵件+密碼找到用戶

我該怎麼辦?

回答

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 

而且很可能配置/初始化/ devise.rb需要像水木清華:

config.authentication_keys = [ :email, :password ] 
+0

方法self.find_for_database_authentication在用戶模型類和參數不可訪問 – maxs 2012-01-18 09:15:02

+0

我的意思是你可以使用User.new獲得encrypted_pa​​ssword表單設計與您擁有的憑據。編輯答案以匹配您的評論 – alony 2012-01-18 09:27:23

+0

這是在設計控制器(在寶石中),而不是在我的控制器中 – maxs 2012-01-18 09:32:02

2

試試這個:

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

+ 1(爲我工作) – 3lviend 2014-08-20 04:58:01

相關問題