简体   繁体   中英

Bcrypt's compare is not working in Node.Js

I was following the tutorial from Dev.to. But I stucked on this: bcrypt's compare not working The code:

const isMatch = await bcrypt.compare(password, user.password);
  if (!isMatch){
    return res.status(400).json({
      msg: "incorrect password"
    });
  }
  

Is the string coming from user.password a hash?

bcrypt compares your raw string with a hash. Here's a simple working example that you can run in a node file.


const bcrypt = require('bcrypt');

const bcryptTest = async () => {
  try {
    const password = 'mypassword';
    const userPass = await bcrypt.hash('mypassword', 5);
    const isMatch = await bcrypt.compare(password, userPass);
    console.log(isMatch) // returns true
  } catch (e) {
    console.log(e)
  }
}

bcryptTest();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM