Reputation: 29
I am new to node js, I am trying to validate a password that is encrypted, and I have given the below code which I have tried.
async function passCheck(event) {
// in event i am passing user entered password and email
var EnteredPassword = bcrypt.hashSync(event.password, 10); //10 saltrounds
var fromDB = await pool.query('SELECT password from User WHERE email = ?', event.emailID);
if (EnteredPassword == fromDB) {
//Here i am comparing
console.log('valid');
} else {
console.log('invalid');
}
}
Upvotes: 0
Views: 6001
Reputation: 35513
bcrypt
has a built-in method for comparing hashes.
async function passCheck(event) {
var fromDB = await pool.query('SELECT password from User WHERE email = ? Limit 1', event.emailID);
// --------------------------------------------------------------------------^
// Added limit 1 to make sure the only one record will be returned.
if (fromDB.length > 0 && await bcrypt.compare(event.password, fromDB[0].password)) {
//Here i am comparing
console.log('valid');
} else {
console.log('invalid');
}
}
DB result sets usually returns an array of objects, therefore, you should check that there are some results (fromDB.length > 0
) and then pass to the compare method the hashed string itself.
Upvotes: 1
Reputation: 11
try this
const auth = await bcrypt.compare(EnteredPassword , fromDB)
if(auth ){
console.log("valid");
}
else{
console.log("invalid")
}
Upvotes: 0