简体   繁体   English

如何在节点中使用 bcryptjs 散列密码?

[英]How to hash password with bcryptjs in node?

I recieve password through req.body and hash it like this :我通过 req.body 收到密码并像这样散列它:

    const { name, email, password, number } = req.body;

    let userExist;
    userExist = await User.find({ email: email });

    if (userExist.length) {
        const err = new Error('User already exists');
        return next(err);
    }

    let hashedPass;
    try {
        hashedPass = await bcrypt(req.body.password, 12);
    } catch (e) {
        console.log('error here bro');
        return next(e);
    }

    const createdUser = new User({
        name,
        email,
        password: hashedPass,
        number,
        ads: []
    });

It console logs 'error here bro' and I dont know why .它控制台记录“错误在这里兄弟”,我不知道为什么。

How can I hash password coming from user with bcrypt js ?如何使用 bcrypt js 散列来自用户的密码?

You can also add salt to your password in the following way:您还可以通过以下方式在密码中添加盐:

const salt = await bcrypt.genSalt(7);
const hashPassword = await bcrypt.hash(req.body.secret, salt);

通过在 bcrypt 后添加 hash 解决了问题

bcrypt.hash(....)

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

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