const comparePassword = async (password, hash) => {
try {
// Compare password
return await bcrypt.compare(password, hash);
} catch (error) {
console.log(error);
}
// Return false if error
return false;
};
//use case
(async () => {
// Hash fetched from DB
const hash = `$2b$10$5ysgXZUJi7MkJWhEhFcZTObGe18G1G.0rnXkewEtXq6ebVx1qpjYW`;
// Check if password is correct
const isValidPass = await comparePassword('123456', hash);
// Print validation status
console.log(`Password is ${!isValidPass ? 'not' : ''} valid!`);
// => Password is valid!
})();
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter