如何解决使用bcrypt散列密码
在插入数据库之前,我需要对密码进行哈希处理,我具有bcrypt的功能,但是我不知道如何将其转换为const并用于将其插入MysqL。
我正在使用bcrypt,那么我应该遵循什么方法来对密码进行哈希处理?
async postCompletedetails(req,res) {
const company = req.params.company;
const name = req.params.name;
const password = req.params.password;
bcrypt.hash(password,saltRounds,(err,hash) => {
});
if (
company !== undefined &&
name !== undefined &&
password !== undefined
) {
const { token } = req.headers;
const decoded = jwt.verify(token,process.env.JWT_ACCOUNT_ACTIVATION);
const id = decoded.id;
const update = await pool.query(
`UPDATE user SET Name_user= '${name}',password= '${password}' WHERE ID_user = ${id}`
);
const incompany = await pool.query(
`INSERT INTO company (Name_company) VALUES ('${company}') `
);
const inrelcompany = await pool.query(
`INSERT INTO rel_company_user (ID_company,ID_user) VALUES (LAST_INSERT_ID(),${id})`
);
return res.json({
code: 200,message: "todo bien... todo correcto y yo que me alegro",password,});
} else {
return res.json({
code: 400,message: "bro hiciste algo mal",});
}
}
解决方法
async postCompletedetails(req,res) {
const company = req.params.company;
const name = req.params.name;
const password = req.params.password;
const saltRounds = 10;
if (
company !== undefined &&
name !== undefined &&
password !== undefined
) {
const hashPass = bcrypt.hash(password,saltRounds,(err,hash) => {
if (err)
{
return err;
}
return hash;
});
hashPass // this will be what you insert into the database.
}
,
好的,我知道了,但是问题是我无法获取哈希密码,hassPass具有未定义的值,我应该更改什么?
const hashPass = await bcrypt.genSalt(saltRounds,function (err,salt) {
if (err) {
throw err
} else {
bcrypt.hash(password,salt,function(err,hash) {
if (err) {
throw err
} else {
console.log(hash)
}
})
}
})
密码被散列,现在我输入控制台密码。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。