【问题标题】:NodeJS: bcrypt is returning false even when the password is correctNodeJS:即使密码正确,bcrypt 也会返回 false
【发布时间】:2020-07-24 18:11:37
【问题描述】:

我的代码的 bcrypt.compare 部分有问题。我的路线能够散列密码并将密码存储在数据库中。该数据库能够存储 255 个字符,并且我已验证密码长度为 60 个字符。每次我将密码与数据库上的散列密码进行比较时,我都会从 bycrypt.compare 返回一个错误。

有没有人遇到过这个并且知道我做错了什么?

在数据库中创建用户的Auth Route:

    app.post('/register/local', async (req, res) => {
    const hashedPassword = await bcrypt.hash(req.body.password, 10) || undefined
    const existingLocalUser = await db.User.findOne({ where: { email: req.body.email } }) || undefined
      if (!existingLocalUser) {
          try {
            const newUser = await db.User.create({
                given_name: req.body.given_name,
                family_name: req.body.family_name,
                email: req.body.email,
                password: hashedPassword,
              }
              )
              res.redirect('/login')
          } catch {
              res.redirect('/register')
          }
      } else if (existingLocalUser.dataValues.google_id) {
        const updateUser = await db.User.update(
            { password: hashedPassword },
            { where: { email: req.body.email } }
          )
        } else {
            console.log("You already have an account. Please login.")
            res.redirect('/login');
        }
})

来自 Passport 的本地策略:

passport.use(new LocalStrategy( async (username, password, done) => {
  const existingLocalUser = await User.findOne({ where: { email: username }})
      if (!existingLocalUser) {
        console.log("No user exisits")
        return done(null, false)
      }
          console.log("password", password)
          console.log("existingLocalUser.password", existingLocalUser.password)
          await bcrypt.compare(password, existingLocalUser.dataValues.password, function(error, result) {
            if (error) {
              return done(error)
            } else if (result) {
              return done(null, existingLocalUser)
            } else {
              return done(null, false)
            }
          })
  }
));

【问题讨论】:

    标签: sql node.js passport.js bcrypt


    【解决方案1】:

     bcrypt.compare(password, existingLocalUser.password, function(error, result) {
                if (error) {
                  return done(error)
                } else if (result) {
                  return done(null, existingLocalUser)
                } else {
                  return done(null, false)
                }
              })

    您正在尝试同时使用回调和等待,请移除等待并坚持使用回调,或者您重构并单独使用异步等待

    【讨论】:

    • 感谢您对此的见解。我删除了等待,但我仍然收到错误的返回..还有其他想法吗?
    【解决方案2】:

    正如@cristos 正确指出的那样,问题可能是您混淆了异步/等待和回调。坚持一种模式。 这是你的代码在 async/await 下的表现,

    try {
      const result = await bcrypt.compare(password, existingLocalUser.password);
    
      if (result) {
        return done(null, existingLocalUser);
      } else {
        return done(null, false);
      }
    } catch (error) {
      return done(error);
    }
    
    
    

    另外,你确定你比较的是正确的值吗? 按照您提供的代码示例,我可以看到您正在记录,

    console.log("password", password)
    console.log("existingLocalUser.password", existingLocalUser.password)
    

    但是,bcrypt.compare() 中比较的值不同,

    bcrypt.compare(password, existingLocalUser.dataValues.password)
    

    【讨论】:

    • 我也试过了,不幸的是,结果仍然返回 false。
    • @KevinT 我已经用更多细节更新了答案
    • 啊,是的,很抱歉。 existingLocalUser.dataValues.password 和 existingLocalUser.password 返回相同的值。我正在测试两种方式,这就是为什么你会看到它。
    【解决方案3】:

    我知道为什么它不工作了……React 或 Redux 用星号掩盖密码,所以把它改成一串星号,然后散列……

    【讨论】:

      猜你喜欢
      • 2020-10-06
      • 2018-02-11
      • 1970-01-01
      • 2016-05-28
      • 2018-02-01
      • 2018-11-28
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多