【发布时间】:2021-04-02 12:05:07
【问题描述】:
大家好,我想在我的 express-mongoose 服务器中使用 bcrypt 对我的 put 请求进行哈希处理
put 请求
// updating a user
router.put('/:id', async (req, res) => {
const {error} = validate(req.body)
if (error) return res.status(400).send(error.details[0].message)
const user = await User.findByIdAndUpdate(req.params.id, {
$set : {
name: req.body.name,
email: req.body.email,
password: req.body.password
}
})
// hashing user passwords
const salt = await bcrypt.genSalt(10)
user.password = await bcrypt.hash(user.password, salt)
if (!user) return res.status(404).send('User with that id does not exist')
res.send(user)
})
除了散列更新的密码之外,更新请求中的所有其他功能都可以正常工作。作为新手,我需要您的帮助/最佳方法建议。 提前谢谢...
【问题讨论】:
-
您是否尝试对纯文本密码进行哈希处理并在数据库中进行更新?如果是,您需要在 findByIdAndUpdate 之前对其进行哈希处理
标签: express mongoose put bcrypt