Using Devise to verify an email address and password

How to go about using Devise to do verification on user credentials

Devise is a very powerful tool for handling authentication in Ruby applications, so powerful in fact that sometimes when I need to do simple tasks I found I get a little drowned in the weight of people talking about use cases far more complex than the one I want to fulfill.

One such example is verifying an email address and password. I don’t need to log the user in or associate them with any role or anything, I just want to know if the submitted email/password combination is a valid one.

My mistake was I was searching for help by searching for things like “Check Sign In Details” or “Verify Log In Details”, which were returning results relating to a method called sign_in, which is very useful but not what I needed here.

Here is the code to verify if an email address and password combo is valid, this assumes that you have devise already set up and working correctly on your user model.

user = User.find_by_email(params[:email])
if user.valid_password?(params[:password])
  # Valid!
else
  # Not Valid!
end

Pretty straightforward!


Recent posts View all

Ruby

Forcing a Rails database column to be not null

How you can force a table column to always have something in it with Rails

Writing Marketing

We've deleted an article's worth of unhelpful words

We've improved several pages across our site by removing words that add no value, and often detract from the article.