Update middleware/midware.js
Browse files- middleware/midware.js +26 -1
middleware/midware.js
CHANGED
@@ -1,9 +1,33 @@
|
|
1 |
import { Database } from '../database/database.js';
|
2 |
import { MongoStorage } from "@canmertinyo/rate-limiter-mongo";
|
3 |
import { rateLimiter } from "@canmertinyo/rate-limiter-core";
|
4 |
-
|
5 |
import * as config from '../config.js';
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
const authenticateApiKey = async (req, res, next) => {
|
8 |
const apiKey = req.headers['x-api-key'];
|
9 |
const dbClient = new Database("AkenoXJs");
|
@@ -102,5 +126,6 @@ class CheckMilWare {
|
|
102 |
export {
|
103 |
CheckMilWare,
|
104 |
authenticateApiKey,
|
|
|
105 |
apiLimiter
|
106 |
};
|
|
|
1 |
import { Database } from '../database/database.js';
|
2 |
import { MongoStorage } from "@canmertinyo/rate-limiter-mongo";
|
3 |
import { rateLimiter } from "@canmertinyo/rate-limiter-core";
|
4 |
+
import { ApiKey } from '../models.js';
|
5 |
import * as config from '../config.js';
|
6 |
|
7 |
+
const authenticateApiKeyPremium = async (req, res, next) => {
|
8 |
+
const apiKey = req.headers['x-api-key'];
|
9 |
+
|
10 |
+
if (!apiKey) {
|
11 |
+
return res.status(401).json({ error: 'Premium API Key required' });
|
12 |
+
}
|
13 |
+
|
14 |
+
try {
|
15 |
+
const keyData = await ApiKey.findOne({ key: apiKey, type: "premium" });
|
16 |
+
|
17 |
+
if (!keyData) {
|
18 |
+
return res.status(403).json({ error: 'Invalid or non-premium API Key' });
|
19 |
+
}
|
20 |
+
|
21 |
+
if (keyData.expiresAt && new Date() > keyData.expiresAt) {
|
22 |
+
return res.status(403).json({ error: 'Premium API Key has expired' });
|
23 |
+
}
|
24 |
+
|
25 |
+
next();
|
26 |
+
} catch (err) {
|
27 |
+
res.status(500).json({ error: 'Server error' });
|
28 |
+
}
|
29 |
+
};
|
30 |
+
|
31 |
const authenticateApiKey = async (req, res, next) => {
|
32 |
const apiKey = req.headers['x-api-key'];
|
33 |
const dbClient = new Database("AkenoXJs");
|
|
|
126 |
export {
|
127 |
CheckMilWare,
|
128 |
authenticateApiKey,
|
129 |
+
authenticateApiKeyPremium,
|
130 |
apiLimiter
|
131 |
};
|