const jwt = require("jsonwebtoken");
const jwtSecret = process.env.JWTSECRET;

exports.adminAuth = async (req, res, next) => {
  try {
    const { auth_token } = req.headers;

    if (!auth_token) {
      return res.status(403).json({ success: false, msg: "Forbidden" });
    }

    const verify = jwt.verify(auth_token, jwtSecret);

    if (!verify) {
      return res.status(403).json({ success: false, msg: "Forbidden" });
    }

    req.user = verify;

    next();
  } catch (error) {
    return res
      .status(500)
      .json({ success: false, msg: "Something went wrong!" });
  }
};
