var jwt = require("jsonwebtoken");

const protect = (req, res, next) => {
  try {
    const token = req.headers.authorization.split(" ")[1];
    const decoded = jwt.verify(token, "pksectret");
    req.userData = decoded;
    //   console.log(req.userData)
    next();
  } catch (err) {
    return res.status(401).send({
      message: "Your session is not valid!",
    });
  }
};

const admin = (req, res, next) => {
  if (req.userData.isAdmin === 1)
    //   console.log(req.userData)
    next();
  else {
    res.status(401).json({
      message: "Not authorized",
    });
  }
};

module.exports = { protect, admin };
