feat: add middleware to verify token (API KEY) send by horacius system
parent
3478b7c5b2
commit
26e90c6ea9
|
@ -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;
|
Loading…
Reference in New Issue