Skip to content

Commit f035a85

Browse files
authored
Merge pull request #127 from Kenlachy/security/api-key-verification
security: add simple API key verification for protected routes #36
2 parents 802c5cd + a529358 commit f035a85

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

auth.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Request, Response, NextFunction } from 'express';
2+
3+
/**
4+
* Middleware to verify the admin API key for protected routes.
5+
* It checks for the 'x-api-key' header and compares it to the ADMIN_API_KEY env variable.
6+
*/
7+
export const verifyApiKey = (req: Request, res: Response, next: NextFunction) => {
8+
const apiKey = req.headers['x-api-key'];
9+
const adminApiKey = process.env.ADMIN_API_KEY;
10+
11+
// Verify that the API key exists and matches the environment variable
12+
if (!adminApiKey || apiKey !== adminApiKey) {
13+
return res.status(401).json({
14+
error: 'Unauthorized',
15+
message: 'Invalid or missing API key.'
16+
});
17+
}
18+
19+
next();
20+
};

0 commit comments

Comments
 (0)