简体   繁体   English

Bcrypt 的比较在 Node.Js 中不起作用

[英]Bcrypt's compare is not working in Node.Js

I was following the tutorial from Dev.to.我正在关注 Dev.to 的教程。 But I stucked on this: bcrypt's compare not working The code:但我坚持这一点:bcrypt的比较不起作用代码:

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?来自user.password的字符串是 hash 吗?

bcrypt compares your raw string with a hash. bcrypt 将您的原始字符串与 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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