我正在尝试创建用于更改密码的路由,这是我尝试过的方法,但是它不起作用。我成功了,但是密码没有改变。
router.put("/:username/change-password", function(req, res) {
User
.findOne({
username: req.params.username
}, (err, user) => {
if (err) {
return res.json({
success: false,
message: "Nema usera."
})
} else {
user.comparePassword(req.body.oldPassword, (err, isMatch) => {
if (!isMatch) return res.json({
success: false,
message: "Stara lozinka nije ispravna."
})
if (req.body.newPassword) {
var salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync(req.body.newPassword, salt);
user.password = hash;
user.save((err, user) => {
if (err) return res.status(500).json({
success: false,
message: "There was a problem updating the user."
});
res.status(200).json({
success: true,
message: "Success updating user.",
});
});
}
})
}
})
})