【问题标题】:bcrypt compare with nodejsbcrypt 与 nodejs 比较
【发布时间】:2021-11-11 17:28:43
【问题描述】:

所以我收到了这个错误 Error: Illegal arguments: string, undefined 这是代码

    router.post('/loguser', async (req, res) => {
        const compareHashed = await bcrypt.compare(req.body.password)
        const user = await User.findOne({
            username: req.body.username,
            password: compareHashed
        })
        if (user) {
            console.log('user is found')
        }
        if (!user) {
            console.log('user is not found')
        }
    })

有人知道如何解决这个问题,我知道这是 bcrypt 的问题

【问题讨论】:

  • 你应该先通过用户名获取用户。然后您应该将您的密码与用户密码进行比较。 bcyrpt.compare(req.body.password, user.password, function(err, data) => { // 你的回调 })
  • 你能告诉我们你想在这里做什么,以便为你写出更好的答案吗?
  • 您需要将 2 件事传递给 bcrypt.compare -- 否则您将如何说出您要比较的 2 件事?
  • @douscriptist 你不必指定回调函数——在这种情况下你会得到一个 Promise 回复你一个await 就像@evan_tech1234 正在做的那样。

标签: node.js backend bcrypt mern


【解决方案1】:

嗨,兄弟,我们首先对密码进行哈希处理并存储在数据库中

从数据库中检索散列密码并将其与实际密码进行比较

例如:如果我们尝试使用电子邮件和密码登录

    User.find({email:req.body.email}).exec().then(result=>{

  if(result.length < 1){
    console.log('email not found')
  }else{
    bcrypt.compare(req.body.password,result[0].password,(err,result)=>{
      if(err){
        console.log('password not match')
      }
      if(result){
        console.log('password match')
      }
    })
  }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-10
    • 2021-11-25
    • 2014-12-25
    • 1970-01-01
    • 2015-08-10
    • 2019-07-10
    • 1970-01-01
    • 2021-04-11
    相关资源
    最近更新 更多