Async function for bcrypt.compare()

PHOTO EMBED

Sun Jun 06 2021 15:47:09 GMT+0000 (Coordinated Universal Time)

Saved by @hisam #bcrypt #authentication #express #nodejs #password

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!
})();
content_copyCOPY

Using bcrypt asynchronously to avoid dumb synchronous bugs

https://attacomsian.com/blog/nodejs-password-hashing-with-bcrypt