I am trying to make login functionality While I was creating the login function like this
login: (req,res) => {
const body = req.body;
getUserByEmail(body.email,(err,results)=>{
if(err){
console.log(err);
}
if(!results){
return res.json({
sucess: 0,
data: "Invalid email or password 1"
});
}
console.log(body.pw);
console.log(results.pw);
const result = compareSync(body.pw,results.pw);
console.log(result)
if(result){
results.pw = undefined;
const jsontoken = sign({result: results},"1234413",{
expiresIn: "1h"
});
return res.json({
sucess: 1,
message: "login sucessfully",
token: jsontoken
});
}else{
return res.json({
sucess: 0,
data: "Invalid email or password2"
});
}
});
}
I console log the body.pw and results.pw but seems like it give me same strings but I was not sure why it gives me false result even if I have same string for body.pw and results.pw
CodePudding user response:
compareSync compares a password with a hash. You first need to hash the password before comparing it with the plain-text password.
First run
bcrypt.hash(myPlaintextPassword, saltRounds).then(function(hash) {
// Store hash in your password DB.
});
Then you can compare
bcrypt.compare(myPlaintextPassword, hash).then(function(result) {
// result == true
});