intern-Assignment/PostgreSQL/formPrac/middlewares/userAuthentication.js
2025-01-31 15:20:39 +05:30

23 lines
496 B
JavaScript

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;