randydev commited on
Commit
59c12a7
·
verified ·
1 Parent(s): 3b030d7

Update middleware/midware.js

Browse files
Files changed (1) hide show
  1. middleware/midware.js +34 -1
middleware/midware.js CHANGED
@@ -1,4 +1,33 @@
1
  import { Database } from '../database/database.js';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  class CheckMilWare {
4
  constructor() {
@@ -47,4 +76,8 @@ class CheckMilWare {
47
  }
48
  }
49
 
50
- export { CheckMilWare };
 
 
 
 
 
1
  import { Database } from '../database/database.js';
2
+ import { rateLimit } 'express-rate-limit';
3
+ import { v4: uuidv4 } from 'uuid';
4
+
5
+ const authenticateApiKey = async (req, res, next) => {
6
+ const apiKey = req.headers['x-api-key'];
7
+ const dbClient = new Database("AkenoXJs", "FastJsAPI");
8
+ const collection = dbClient.collection()
9
+
10
+ if (!apiKey) {
11
+ return res.status(401).json({ error: 'API Key required' });
12
+ }
13
+
14
+ try {
15
+ const keyDoc = await collection('apiKeys').findOne({ key: apiKey });
16
+ if (!keyDoc) {
17
+ return res.status(403).json({ error: 'Invalid API Key' });
18
+ }
19
+ next();
20
+ } catch (err) {
21
+ res.status(500).json({ error: 'Server error' });
22
+ }
23
+ };
24
+
25
+ const apiLimiter = rateLimit({
26
+ windowMs: 15 * 60 * 1000,
27
+ max: 100,
28
+ keyGenerator: (req) => req.headers['x-api-key'], // Limit per API Key
29
+ message: 'Too many requests from this API Key'
30
+ });
31
 
32
  class CheckMilWare {
33
  constructor() {
 
76
  }
77
  }
78
 
79
+ export {
80
+ CheckMilWare,
81
+ authenticateApiKey,
82
+ apiLimiter
83
+ };