How to check the user password in the program

I am having trouble matching user password using devem gem in rails. The user password stored on my db, which is encrypted_password, and I try to find the user by password, but I do not understand how to combine the password from the form and encrypted_password in my db.

User.find_by_email_and_password(params[:user][:email], params[:user][:password]) 
+88
ruby-on-rails devise
Dec 01 '10 at 3:27
source share
5 answers

I think this is a better and more elegant way to do this:

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

Another method in which you generate a digest from a user instance gives me protected method errors.

+243
Dec 06 '10 at 19:23
source share

Use development methods

Devise provides you with built-in user password verification methods :

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



For Rails 4+ with strong pairs, 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 
+13
Feb 04 '17 at 22:35
source share

I think this one will be the best

 valid_password = User.find_by_email(params[:user][:email]).valid_password?(params[:user][:password]) 
+5
May 6 '14 at 9:00
source share
 user = User.find_by_email_and_password(params[:user][:email]) user.valid_password?(params[:user][:password]) 

@joshaidan is the right answer

+1
Apr 24 '15 at 4:36
source share

I would suggest this.

 user = User.where("email=? OR username=?", email_or_username, email_or_username).first.valid_password?(user_password) 
0
May 2 '16 at 19:03
source share



All Articles