简体   繁体   English

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

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

I'm using bcrypt.hash in a password, and this hash normal... but when i save this hashed password in a mongodb with a mongoose, this not the same passowrd when i hash the password. 我在密码中使用bcrypt.hash,并且这个哈希正常......但是当我在mongodb中使用mongoose保存这个哈希密码时,当我对密码进行哈希时,这不是相同的passowrd。

Example: 例:

password hash: $2b$10$bUY/7mrZd3rp1S7NwaZko.ShDFj47rAfdGHG1QcQxGdtvzaDd.WH2 密码哈希:$ 2b $ 10 $ bUY / 7mrZd3rp1S7NwaZko.ShDFj47rAfdGHG1QcQxGdtvzaDd.WH2

password saved mongo: $2b$10$fOLYjjib7ycRbq7BqzNdMuPNbTPjMIVAZ1QQzBvX5cMEhi6rERjJK 密码保存mongo:$ 2b $ 10 $ fOLYjjib7ycRbq7BqzNdMuPNbTPjMIVAZ1QQzBvX5cMEhi6rERjJK

My signup user code: 我的注册用户代码:

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),
});

My login user code: 我的登录用户代码:

const passOk = await bcrypt.compare(password, user.password);
    if (!passOk) {
    Logs.logError(item.path, {
        ...item,
        error: "Error",
});

My password in user schema: 用户架构中的密码:

password: {
    type: String,
    required: true,
    select: false,
},

When i compare, the password always not equal 当我比较时,密码总是不相等

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

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

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