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;
