// Login
app.post("/login", async (req, res) => {
try{
// Destructure Req Body
const { email, password } = req.body;
// Validate Body
if (!email || !password) {
res.status(400).json({ success: false, message: "PARAMS_MISSING" });
return // stop execution of the function
}
// Build the SQL query
const query = `SELECT * FROM user WHERE email = "${email}"`;
// Get the user from DB
const user = await db(query);
// Check if password is valid
const isPasswordValid = decryptPassword(user.hash_password, password);
// Return if password is not valid
if (!isPasswordValid) {
res.status(401).json({ success: false, message: "INAVLID_PASSWORD" });
return; // stop exec of the function
}
// Generate Token
const token = generatePassword({ id: user.id, email: user.email });
// Save Cookie
res.cookie("token", token, { maxAge: 900000, httpOnly: true });
// Return
res.json({ success: true, message: "USER_AUTHENTICATED" });
res.end();
} catch(err) {
console.error(err) // code to handle the err
}
});
not getting in error in terminal n also not able to log in using postman for HTTP request n it's showing password_invalid as I coded ! will some help me?