I am using node, mysql as the server and DB and ionic on the front end. I've managed to register a user with a hash using bcrypt and attempting to authenticate the user to log them in.
When comparing the password that user enters, in bcryptJS it seems like we cannot decrypt their password. When I console.log password and the result of user lookup in my db, I am comparing the password that the user enters with the hash that's stored so i am always retuning a 400 status to the front end.
Authentication code:
app.post('/login', function(req, res) {
connection.connect(function(err) {
let email = req.body.email;
let password = req.body.password;
connection.query("SELECT * FROM sometable WHERE username = ? ", [email], function(error, results, fields) {
bcrypt.compare(req.body.password, results[0].password, function(err, result) {
console.log('>>>>>> ', password)
console.log('>>>>>> ', results[0].password)
if(result) {
return res.send();
}
else {
return res.status(400).send();
}
})
});
});
});
What's the proper way to compare the password user enters with the hash that's stored in my db?
Thanks for your help.
edit:
I've tried the below code (adding a password strings) and I'm still getting the false result... What am I missing here?
bcrypt.compare('somePassword', 'somePassword', function(err, res) {
if(res) {
console.log('true')
} else {
console.log('false')
}
});
err? - jcaron