Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify a user's password in Devise

I'm having a problem matching user password using devise gem in rails. User password stored on my db which is encrypted_password and i am trying to find user by password, but I don't understand how to match password from form and encrypted_password in my db.

User.find_by_email_and_password(params[:user][:email], params[:user][:password]) 
like image 277
poporul Avatar asked Dec 01 '10 03:12

poporul


2 Answers

I think this is a better, and more elegant way of doing it:

user = User.find_by_email(params[:user][:email]) user.valid_password?(params[:user][:password]) 

The other method where you generate the digest from the user instance was giving me protected method errors.

like image 106
joshaidan Avatar answered Sep 29 '22 19:09

joshaidan


Use Devise Methods

Devise provides you with built-in methods to verify a user's password:

user = User.find_for_authentication(email: params[:user][:email])  user.valid_password?(params[:user][:password]) 

For Rails 4+ with Strong Params, you can do something like this:

def login   user = User.find_for_authentication(email: login_params[:email])    if user.valid_password?(login_params[:password])     user.remember_me = login_params[:remember_me]     sign_in_and_redirect(user, event: :authentication)   end end  private def login_params   params.require(:user).permit(:email, :password, :remember_me) end 
like image 20
Sheharyar Avatar answered Sep 29 '22 19:09

Sheharyar