feat: add middleware to verify token (API KEY) send by horacius system

feat-scaling-ticket-remote-creation
Henrriky 2024-03-12 17:07:41 -03:00 committed by Henrriky
parent 3478b7c5b2
commit 26e90c6ea9
1 changed files with 23 additions and 0 deletions

View File

@ -0,0 +1,23 @@
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_REMOTE_TICKET_CREATION
if (!apiKeyIsValid) {
throw new AppError(
"Invalid token",
401
);
}
return next();
};
export default verifyAPIKey;