简体   繁体   中英

Find user using email + password in Devise

My Rails app uses data from legacy database. Imported users from this DB contain duplicate emails. Authentication doing with email+password (and it's a unique combination in DB). Devise uses method find_for_database_authentication to find user. However params don't contain password (just login name).

What can I do?

try this :

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

You can search this way

in the User model override the find method:

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

And probably config/initializers/devise.rb will require smth like:

 config.authentication_keys = [ :email, :password ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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