我正在尝试更新用户信息。用户创建的帐户只有名称电子邮件和密码,我正在尝试使用其他可选字段更新用户信息,如果不使用数据库中的旧密码,我如何检查密码是否已更改更新密码。我在我的前端使用 react。我正在使用 sequilize orm - mysql。我怎么能做到这一点。当我什至尝试用新密码覆盖密码时,它给了我一个错误。请帮助。谢谢
router.post("/update", (req, res) => {
const dataToday = new Date();
const studentData = {
name: req.body.name,
email: req.body.email,
password: req.body.password,
phoneNumber: req.body.phoneNumber,
about_me: req.body.about_me,
city: req.body.city,
country: req.body.country,
company: req.body.company,
school: req.body.school,
hometown: req.body.hometown,
gender: req.body.gender,
created: dataToday
};
console.log(studentData);
Student_Info.findOne({
where: {
email: req.body.email
}
})
.then(student => {
if (student) {
bcrypt.hash(req.body.password, 10, (err, hash) => {
studentData.password = hash;
Student_Info.update(studentData)
.then(student => {
res.json({
status: student.email + "updated"
});
})
.catch(err => {
res.send("error" + err);
});
});
} else {
res.json({
error: "No User Found"
});
}
})
.catch(err => {
res.send("error" + err);
});
});
最佳答案
尝试使用 compareSync
怎么样?
Student_Info.findOne({
where: {
email: req.body.email
}
})
.then(student => {
if (!student) throw new Error("No User Found")
// return true if old password equals new password
const validPassword = bcrypt.compareSync(req.body.password, student.password)
if (validPassword) throw new Error("Same Password")
studentData.password = bcrypt.hashSync(req.body.password, 10)
return student.update(studentData)
})
.then(student => {
res.json({
status: student.email + "updated"
});
})
.catch(err => {
res.send("error" + err);
});
关于reactjs - 更新用户信息 sequelize express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55120229/