![](/img/trans.png)
[英]Bcrypt unable to work, Bcrypt.compareSync always return false
[英]Bcrypt.compareSync always returning false
嗨,所以我正在嘗試使用 sequelize 創建我的第一個登錄名,並且我正在努力處理散列和比較散列,它總是返回 false。 由於我正在學習,我認為我在哈希或比較上做錯了。 我正在使用 SQL 數據庫
這是我的登錄代碼,我正在使用 express session 並續集:
'processLogin': (req, res) => {
db.User.findOne({
where: {
email: req.body.email
}
})
.then(async user => {
var eSession = req.session.userLogin
let checkPass = await bcrypt.compare(req.body.password, user.password)
console.log(checkPass);
if(checkPass){
eSession = user;
res.send("Success");
}else{
res.render('login', {errors: [{
msg: "Incorrect password"
}]});
}
if(user == undefined){
res.render('login', {errors: [{
msg: "User not found, please Register"
}]});}
})
}
這里是我實際上在我的寄存器上散列密碼的地方:
'save': async (req, res) => {
var a = [req.body.fullname, req.body.email, req.body.number, bcrypt.hashSync(req.body.password, 10), bcrypt.hashSync(req.body.repassword, 10)];
let errors = validationResult(req);
if(errors.isEmpty()){
db.User.create({
full_name: a[0],
email: a[1],
phone_number: a[2],
password: await bcrypt.hash(a[3], 10),
confirm_password: await bcrypt.hash(a[4], 10)
})
.then(users => {
res.send("succes!!");
})
}else{
res.render('register', { errors: errors.errors })
}
}
}
安裝同步為什么不嘗試異步並等待它得到散列或解密。
內部異步 function 到 hash 密碼。
let hashedPassword = await hash(password, 10);
和內部異步 function 用於比較密碼
let checkPass = await compare(password, foundUser.password);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.