i keep getting this error “Invalid password” even if the password and email are correct which makes no sense pls help me to solve this problem i think that i am comparing a hashed password and a plain text password but i dont know how to solve it
app.post('/login0', (req, res) => {
const { email, password } = req.body;
const sql = "SELECT * FROM user WHERE email = ?";
const values = [email];
db.query(sql, values, (err, result) => {
if (err) {
console.error("Database error: ", err);
return res.status(500).json({ message: "Login failed: Database error" });
}
if (result.length > 0) {
const storedPassword = result[0].password;
if (bcrypt.compareSync(password, storedPassword)) {
console.log("Password match, login successful");
return res.status(200).json({ message: "Login successful", user: result[0] });
} else {
console.log("Invalid password");
return res.status(401).json({ message: "Invalid password" });
}
} else {
console.log("Account not found");
return res.status(401).json({ message: "Account not found" });
}
});
});