繁体   English   中英

Bcrypt哈希密码与Mongodb中保存的密码哈希不同

[英]Bcrypt hash password is not the same of password hash saved in Mongodb

我在密码中使用bcrypt.hash,并且这个哈希正常......但是当我在mongodb中使用mongoose保存这个哈希密码时,当我对密码进行哈希时,这不是相同的passowrd。

例:

密码哈希:$ 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,
},

当我比较时,密码总是不相等

出现此问题的原因是我的用户模型具有带有bcrypt哈希的pre。(“save”),这与router.js中的bcrypt哈希冲突

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM