create-node-mongo-express-app
Version:
Node Js Template with MVC pattern, typescript, mongoose and express
24 lines (18 loc) • 494 B
text/typescript
import { NextFunction, Request, Response } from 'express';
import dotenv from 'dotenv';
import { ApiError } from '../utils/ApiError';
dotenv.config();
export const verifyApiKey = async (
req: Request,
_: Response,
next: NextFunction,
) => {
const apiKey = req.header('api-key');
if (!apiKey) {
throw new ApiError(401, 'Unauthorized Request');
}
if (apiKey !== process.env.API_KEY) {
throw new ApiError(401, 'Invalid API Key');
}
next();
};