我的输入密码和数据库密码相同,然后函数返回false。这里的compare方法不能正常工作,每次我提供正确的密码时都会得到错误的回报。
app.post('/login', (req, res) => {
const username = req.body.username;
const password = req.body.password;
db.query('SELECT * FROM dataa WHERE username = ?',[username], function (error, rows, fields) {
if (error) {
// console.log("error ocurred",error);
res.send({
"code":400,
"failed":"error ocurred"
})
}else{
// console.log('The solution is: ', results);
if(rows.length >0){
console.log(bcrypt.compareSync(password, rows[0].password));
if(bcrypt.compareSync(password, rows[0].password)){
res.send({
"code":200,
"success":"login sucessfull"
});
}
else{
res.send({
"code":204,
"success":"Email and password does not match"
});
}
}
else{
res.send({
"code":204,
"success":"Email does not exits"
});
}
}
});
})
答案 0 :(得分:0)
您的数据库中可能有多个用户名,该用户名与您提供的用户名匹配,因此它将尝试将其与第一个用户名匹配,但是密码将是一个不同的..