intern-Assignment/PostgreSQL/formPrac/middlewares/userAuthentication.js

23 lines
496 B
JavaScript
Raw Normal View History

2025-01-31 09:50:39 +00:00
const jwt = require("jsonwebtoken");
const SECRETKEY = "NOTBKwefw$@!CABHDO@#%*(^kmbidei";
const userAuthentication = (req, res, next) => {
const token =
req.headers.authorization && req.header.authorization.split(" ")[1];
if (!token) {
res.redirect("/login");
} else {
jwt.verify(token, SECRETKEY, (err, decoded) => {
if (err) {
return res.redirect("/login");
}
req.user = decoded;
next();
});
}
};
module.exports = userAuthentication;