const jwt = require("jsonwebtoken");
require("dotenv").config();

const authToken = async (req, res, next) => {
  const authHeader = req.headers["authorization"];
  console.log(authHeader)
  // const token =  authHeader.split(".")[1];
  // console.log(token)
  console.log(process.env.JWT_SECRET)
  if (authHeader === null) {
    return res.status(401).json({ message: "token not found", success: false });
  }
  jwt.verify(authHeader, process.env.JWT_SECRET, (err, id) => {
    if (err)
      return res
        .status(401)
        .json({ message: err, success: false });
    req.id = id;
    next();
  });
};

module.exports = authToken;
