Password hash is working and its storing correctly But while comparing the res is always returning false Even though the password is correct. I am using bcryptjs for hashing
app.post("/api/register", (req, res) => {
const { name, email, school, phone, password } = req.body;
bcrypt.genSalt(10, function (err, salt) {
bcrypt.hash(password, salt, function (err, hash) {
con.query(
`INSERT INTO users(uid, u_name, u_email, u_school, u_phone, u_password) VALUES ('[value-1]','${name}','${email}','${school}','${phone}','${hash}')`,
function (err, result) {
if (err) {
console.log(err);
}
console.log(result);
}
);
});
});
});
app.post("/api/login", (req, res) => {
const { email, password } = req.body;
con.query(
`SELECT * FROM users WHERE u_email='${email}'`,
function (err, result) {
if (err) {
res.send(err.sqlMessage).end();
} else {
bcrypt.compare(password, result[0].u_password).then((res) => {
console.log(res);
});
}
}
);
});
CodePudding user response:
You need to hash the password variable before you compare it in the login route. Otherwise you're comparing a hash and a string and those do not mix.
CodePudding user response:
Thanks for the response. I found the mistake it was in my database. I limit my password varchar(30) but the string size was 60 thats why it was not working