warmbox/src/middleware/apiKeyAuth.ts

18 lines
424 B
TypeScript

import { Request, Response, NextFunction } from 'express';
import { config } from '../config';
import { AppError } from './errorHandler';
export function apiKeyAuth(req: Request, _res: Response, next: NextFunction): void {
if (!config.API_KEY) {
next();
return;
}
const key = req.headers['x-api-key'];
if (key !== config.API_KEY) {
next(new AppError(401, 'Unauthorized'));
return;
}
next();
}