24 lines
576 B
TypeScript
24 lines
576 B
TypeScript
|
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;
|