【发布时间】:2019-09-21 12:33:15
【问题描述】:
我在密码中使用 bcrypt.hash,这个哈希值正常...但是当我用猫鼬将此哈希密码保存在 mongodb 中时,当我对密码进行哈希处理时,这与密码不同。
例子:
密码哈希:$2b$10$bUY/7mrZd3rp1S7NwaZko.ShDFj47rAfdGHG1QcQxGdtvzaDd.WH2
密码保存mongo:$2b$10$fOLYjjib7ycRbq7BqzNdMuPNbTPjMIVAZ1QQzBvX5cMEhi6rERjJK
我的注册用户代码:
req.body.password = await bcrypt.hash(req.body.password, 10);
const user = await User.create(req.body);
Logs.logRequest(item.path, { item });
user.password = undefined;
return res.status(201).send({
user,
token: await createToken(user),
});
我的登录用户代码:
const passOk = await bcrypt.compare(password, user.password);
if (!passOk) {
Logs.logError(item.path, {
...item,
error: "Error",
});
我在用户架构中的密码:
password: {
type: String,
required: true,
select: false,
},
当我比较时,密码总是不相等
【问题讨论】:
-
用 await User.create(req.body)
-
User型号代码中有什么内容? -
问题出在用户模型中
标签: javascript node.js mongoose hash bcrypt