我的输入密码和数据库密码是一样的,那么函数也返回false。这里的比较方法工作不正常。每次提供正确的密码时,我都会得到false作为回报。
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"
});
}
}
});
})
1条答案
按热度按时间1dkrff031#
您的数据库中可能有多个用户名与您提供的用户名匹配,因此它将尝试与第一个用户名匹配,但密码将是另一个。。