import { Request, Response, NextFunction } from "express";
import AppError from "../errors/AppError";
const verifyAPIKey = (req: Request, res: Response, next: NextFunction): void => {
  const authHeader = req.headers.authorization; 

  if (!authHeader) {
    throw new AppError("ERR_SESSION_EXPIRED", 401);
  }

  const [, token] = authHeader.split(" ");  

  const apiKeyIsValid = token === process.env.TOKEN_IAM_HORACIUS_EL
  if (!apiKeyIsValid) {
    throw new AppError(
      "Invalid token",
      401
    );
  }

  return next();
};

export default verifyAPIKey;
