首页 文章

如何在passport-local-mongoose中比较密码

提问于
浏览
0

我想将登录用户的密码与他们从表单提供的密码进行比较,因为我需要确保它确实是用户,因为我希望为他们提供启用2fa的选项,我相信任何人如果用户登录仪表板,则可以启用该功能 . 我使用bcrypt比较密码,但它会引发错误..也使用passport-local-mongoose注册我的用户和进行身份验证 .

router.post("/enableTwoFa/:id", isLoggedIn, isVerified, function(req, res){
if(req.isAuthenticated){
    User.findById(req.params.id, function(err,found){
    if(err){
        res.redirect("back")
        res.send('User not found with the proper ID')
    } else {
        if(req.body.accountPassword){
            console.log(found)
            bcrypt.compare(req.body.accountPassword,found.password, function(err,success){
                if(err){
                    console.log(err)
                    res.send('There was a problem somewhere')
                } else {
                    console.log(success)
                    res.send('password hashed')
                }
            })

        } else {

                res.send('Please input your account password!!')
        }
    }
})
} else {
    res.send('You are not authorized for this request')
}

})

它抛出了丢失数据和散列的错误 . 但我不知道如何找到所需的字段..或者如果有一个功能可以在护照中处理这个,我是新手 .

Error: data and hash arguments required
    at Object.compare (/home/ubuntu/workspace/bit-main/main/node_modules/bcrypt/bcrypt.js:209:17)
    at /home/ubuntu/workspace/bit-main/main/routes/dashboard.js:448:24
    at Query.<anonymous> (/home/ubuntu/workspace/bit-main/main/node_modules/mongoose/lib/model.js:3928:16)
    at /home/ubuntu/workspace/bit-main/main/node_modules/kareem/index.js:297:21
    at /home/ubuntu/workspace/bit-main/main/node_modules/kareem/index.js:135:16
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)

1 回答

  • 0

    在对这个问题做了一些研究之后,我发现在passport-local-mongoose中有一个authenticate()函数来处理这个请求所以不需要使用bcrypt . 它接受3 cb(hashError,model,passwordError) . 所以我决定回答它,因为有人遇到同样的问题 .

    router.post("/enable2fa/:id", isVerified, function(req, res){
        if(req.isAuthenticated){
                User.findById(req.params.id, function(err,found){
                if(err){
                    res.redirect("back")
                    res.send('User not found with the proper ID')
                } else {
                    if(req.body.accountPassword){
                        found.authenticate(req.body.accountPassword, function(err,model,passwordError){
                            if(passwordError){
                                console.log(err)
                                res.send('The given password is incorrect!!')
                            } else if(model) {
                                console.log(`correct password ${model}`)
                                  *run other code stuff*
                                res.send('2fa enabled for this account')
                            }
                        })
                    } else {
                         res.send('Please input your account password!!')
                    }
                }
            })
        } else {
            res.send('You are not authorized for this request')
        }
    
    })
    

相关问题