简体   繁体   中英

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.

Example:

password hash: $2b$10$bUY/7mrZd3rp1S7NwaZko.ShDFj47rAfdGHG1QcQxGdtvzaDd.WH2

password saved 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哈希冲突

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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