I'm trying to compare two passwords , one from DB and other is user entered. The one from db is hashed and stored in binary format. So I have to conver it to string before comparing.
console.log(
user.PasswordHash.toString('base64') ===
'$2a$10$UZptbo5h4lQFYmK352CPf.jDvPZh1pm3uC3Nb0wEVjF/RUCw2OU2G'
); //false
When I look in console user.PasswordHash.toString('base64') evaluates to $2a$10$UZptbo5h4lQFYmK352CPf.jDvPZh1pm3uC3Nb0wEVjF/RUCw2OU2G which is the same value.
What is happening?
bcrypt.compareSync(
password,
user.PasswordHash.toString('base64')); //false
Any help is appreciated.
The problem is not in the code you are sharing.. Is somewhere else.. My thesis is that you have an asynchronous problem meaning that the data is delivered after the code is executed. In that case the variable is undefined and the conditional statement will return false as in the following example:
let x;
function isOne(input){
return input === 1;
}
console.log(isOne(x))
Try implementing a promise https://www.w3schools.com/js/js_promise.asp
To see if it's an asynchronous problem you can use setTimeout()
Make sure you have the data and then execute your condition statement. Go step by step checking with console.log()