const jwt = require("jsonwebtoken");

const adminAuth = (req, res, next) => {
  try {
    const getToken = req.headers.authorization.replace("Bearer ", "");

    if (!getToken) throw "session expired: please login again";
    const verifiedToken = jwt.verify(getToken, process.env.jwt_sec1);
    req.user = verifiedToken;
  } catch (error) {
    res.status(401).json({
      status: "failed",
      error:
        "Access denied due to Invalid or Expired Token, please LOG-IN again",
    });
    return;
  }
  next();
};

module.exports = adminAuth;
