Bcrypt NodeJS compare password always returning false

317 Views Asked by At

So obviously i am using bcrypt to register and hash my password and that works. And now i am making an login where i have to compare the form password and the hashed password in the database and this is how i do this:

// Get post data
let email = req.body.email;
let password = req.body.password;

con.query("SELECT * FROM users WHERE email = ?", email, function (err, result, fields) {
    bcrypt.compare(password, result[0].password, function(err, result) {
        if (err) { throw (err); }
        console.log(result);
    });
});

it always returns false; if i console.log result[0].password, i get my hashed password back so thats good but why does it always return false?

1

There are 1 best solutions below

2
On

I would suggest you to try changing your if statement to:

con.query("SELECT * FROM users WHERE email = ?", email, function (err,
     result, fields) {
         bcrypt.compare(password, result[0].password, function(err, result) {
             if (!result) { throw (err); }
             console.log(result);
         }); 
    });