我正在構建一個應用程式,我正在努力使用護照和 MongoDB 更新密碼。在設定新密碼之前,我想檢查用戶是否輸入了他的實際密碼并且它與存盤在資料庫中的內容匹配。
這是我到目前為止所得到的:
if (req.body.password == req.user.password) {
if (req.body.newPassword.normalize() == req.body.confirmPassword.normalize()) {
// Verifying if the new password matches the confirmation one
// before actually changing the password (This part works)
}
} else {
// Handling if the old password does not match the DB
}
res.redirect('/profile')
我一直在嘗試這樣的事情:
passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({
username: req.user.email
}, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false);
}
if (!user.verifyPassword(req.body.password)) {
return done(null, false);
}
return done(null, user);
});
}
仍然,不起作用......有什么提示嗎?:)
編輯
我一直在嘗試使用加密來獲得與存盤在 MongoDB 中的哈希相同的哈希。要注冊新用戶,我使用護照。
let hash = crypto.createHmac('sha256', secret)
.update('I love cupcakes') // I have no idea of what this this line does, actually...
.digest('hex');
console.log(hash);
我想在某個時候我應該將資料庫存盤的鹽傳遞給一個函式來驗證他提交的密碼與存盤的密碼相同,我只是不知道該怎么做......
uj5u.com熱心網友回復:
嘗試req.body.password
使用秘密對收到的資訊進行散列,看看它是否與保存在資料庫中的已散列的密碼相匹配。如果是,則是相同的舊密碼。
uj5u.com熱心網友回復:
通過大量的互聯網搜索,我想出了這個:
async function fooChangePW(req,res){
req.user.changePassword(req.body.password, req.body.newPassword)
.then(() => {
console.log('password changed');
})
.catch((error) => {
console.log(error);
})
res.redirect('/profile')
}
由于用戶已通過身份驗證,因此可以使用req.user.changePassword(oldPassword, newPassword)
.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/360064.html
標籤:javascript 猫鼬 护照.js