I am looking for some pointers on how to use Crypto::Bcrypt::Password
for authentication in my web application.
When a user signs up, I store the hashed password of the user in Postgres in a text format. The hash is created using (assume that password is abcd
):
Crypto::Bcrypt::Password("abcd")
Now when the user signs in I am trying to use the following function (currently I have added puts
statements for debugging:
def is_email_password_valid(email : String, password : String)
begin
puts "Inputs: #{email}, #{password}"
emaildb, passdb = @conn.query_one("select email, password from users
where email = $1",
email,
as: {String, String})
rescue ex
puts ex.message
else
puts "#{emaildb}, #{passdb}"
if password == Crypto::Bcrypt::Password.new(passdb)
puts "Result: valid email and password"
else
puts "Invalid creds!"
end
end
end
Even if I enter the correct password, the above is giving my Invalid creds!
as output. I am not sure why? Any help about the data format to use and how to compare the two passwords (the plain text entered by the user, and the hashed one in the database) would be appreciated.
Thank you.