forked from BETAIL-BOYS/TradeFlow-Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
20 lines (17 loc) · 656 Bytes
/
Copy pathauth.ts
File metadata and controls
20 lines (17 loc) · 656 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { Request, Response, NextFunction } from 'express';
/**
* Middleware to verify the admin API key for protected routes.
* It checks for the 'x-api-key' header and compares it to the ADMIN_API_KEY env variable.
*/
export const verifyApiKey = (req: Request, res: Response, next: NextFunction) => {
const apiKey = req.headers['x-api-key'];
const adminApiKey = process.env.ADMIN_API_KEY;
// Verify that the API key exists and matches the environment variable
if (!adminApiKey || apiKey !== adminApiKey) {
return res.status(401).json({
error: 'Unauthorized',
message: 'Invalid or missing API key.'
});
}
next();
};