javascript - 如何在更换新护照之前检查密码是否匹配
问题描述
我正在构建一个应用程序,并且正在努力使用护照和 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);
我想在某些时候我应该将数据库存储的盐传递给一个函数,以验证他提交的密码与存储的密码相同,我只是不知道该怎么做......
解决方案
通过大量的互联网搜索,我想出了这个:
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)
.
推荐阅读
- r - 更改数据帧结构(两个数据帧为一个)
- python - 投资组合优化工具的问题 - IndexError
- ruby-on-rails - 带有自定义 JSON 失败消息的 Rails 基本身份验证
- sql - 在一对多系统中存储和计算关系
- javascript - 未捕获的 ReferenceError:未定义 CallApi
- java - 有没有更好的方法来编写这个循环?
- spring - jqgrid 基于特定值的格式。春天的jqgrid
- c# - 在一个表中查找条目以在另一个表中搜索
- java - 如何在类路径中包含一个 jar 文件并在 linux 中运行另一个 jar
- swift - 使 UIColor 可编码 - 符合协议“可编码”