Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I simply verify that a username and password are correct with Devise and Rails 3

I am using Devise and Rails 3.

I want to call a function that will simply verify that a user's username and password combination is correct without actually logging in that user. Is there some way I can accomplish something like this with Devise:

User.authenticate(params[:username], params[:password]) 
like image 465
Adam Singer Avatar asked Jul 16 '10 08:07

Adam Singer


People also ask

What does devise do in Rails?

Devise is the cornerstone gem for Ruby on Rails authentication. With Devise, creating a User that can log in and out of your application is so simple because Devise takes care of all the controllers necessary for user creation ( users_controller ) and for user sessions ( users_sessions_controller ).


2 Answers

Get the user out of the database:

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

Then you can check their password with:

user.valid_password?(params[:password]) 
like image 103
Steven Avatar answered Sep 22 '22 10:09

Steven


Or using the one liner:

User.find_by_email(params[:email]).try(:valid_password?, params[:password]) 

which returns true or nil

like image 28
LiAh Sheep Avatar answered Sep 21 '22 10:09

LiAh Sheep