我正在尝试验证密码我发现的大多数教程都是使用mongodb但是我必须对mysql执行相同操作。只是检查存储在db和用户提供的密码中的密码是否相同我使用compareSync并打印它控制台,但我每次都看到假。
app.post('/register',function(req,res,next){
con.connect();
var username=req.body.username;
var passwordToSave = bcrypt.hashSync(req.body.password);
req.body.password=passwordToSave;
var values=[req.body.username,passwordToSave];
var post={USER_NAME : req.body.username, PASSWORD : passwordToSave};
console.log(passwordToSave);
con.query(insert_sql,post,function(err,result){
if(err){
console.log(err);
}else{
res.json({message : "User successfully registered"});
}
});
con.query('SELECT USER_NAME,PASSWORD FROM USERS WHERE USER_NAME =? ',username,function(err,result,fields){
if(err){
console.log(err);
}else{
console.log(result);
var isPasswordCorrect = bcrypt.compareSync(passwordToSave, result[0].PASSWORD);
console.log(isPasswordCorrect);
}
});