We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 802c5cd + a529358 commit f035a85Copy full SHA for f035a85
auth.ts
@@ -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