I am testing a login/register page. Though the register part is working fine, when trying to login with the same user credentials, the page does not respond at all. On the console, I am getting an 'UnhandledPromiseRejectionWarning' despite using the try-catch block.
(node:8248) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag
--unhandled-rejections=strict(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:8248) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
exports.login = async(req, res) => {
try {
const { email, password } = req.body;
if (!email || !password) {
return res.status(400).render('login', {
message: "PLEASE PROVIDE AN EMAIL AND/OR PASSWORD"
})
}
db.query('SELECT * FROM users WHERE email = ?', [email], async(error, results) => {
console.log(results);
if (!results || !(await bcrypt.compare(password, results[0].password))) {
res.status(401).render('login', {
message: 'EMAIL OR PASSWORD IS INCORRECT'
})
} else {
const id = results[0].id;
const token = jwt.sign({ id }, process.env.JWT_SECRET, {
expiresIn: process.env.JWT_EXPIRES_IN
});
console.log("THE TOKEN IS " + token);
const cookieOptions = {
expires: new Date(
Date.now() + process.env.JWT_COOKIE_EXPIRES * 24 * 60 * 60 * 1000
),
httpOnly: true
}
res.cookie('jwt', token, cookieOptions);
res.status(200).redirext("/");
}
})
} catch (error) {
console.log(error);
}
}
Any kind of assistance would be of great help. Thanks!:)