Update middleware/midware.js
Browse files- middleware/midware.js +5 -5
middleware/midware.js
CHANGED
@@ -3,14 +3,14 @@ import { rateLimit } from 'express-rate-limit';
|
|
3 |
|
4 |
const authenticateApiKey = async (req, res, next) => {
|
5 |
const apiKey = req.headers['x-api-key'];
|
6 |
-
const dbClient = new Database("AkenoXJs"
|
7 |
-
const db = dbClient.collection()
|
8 |
if (!apiKey) {
|
9 |
return res.status(401).json({ error: 'API Key required' });
|
10 |
}
|
11 |
|
12 |
try {
|
13 |
-
const keyDoc = await db
|
14 |
if (!keyDoc) {
|
15 |
return res.status(403).json({ error: 'Invalid API Key' });
|
16 |
}
|
@@ -23,13 +23,13 @@ const authenticateApiKey = async (req, res, next) => {
|
|
23 |
const apiLimiter = rateLimit({
|
24 |
windowMs: 15 * 60 * 1000,
|
25 |
max: 100,
|
26 |
-
keyGenerator: (req) => req.headers['x-api-key'],
|
27 |
message: 'Too many requests from this API Key'
|
28 |
});
|
29 |
|
30 |
class CheckMilWare {
|
31 |
constructor() {
|
32 |
-
this.dbClient = new Database("AkenoXJs"
|
33 |
}
|
34 |
|
35 |
async handle(req, res, next) {
|
|
|
3 |
|
4 |
const authenticateApiKey = async (req, res, next) => {
|
5 |
const apiKey = req.headers['x-api-key'];
|
6 |
+
const dbClient = new Database("AkenoXJs");
|
7 |
+
const db = dbClient.collection("api_keys");
|
8 |
if (!apiKey) {
|
9 |
return res.status(401).json({ error: 'API Key required' });
|
10 |
}
|
11 |
|
12 |
try {
|
13 |
+
const keyDoc = await db.findOne({key: apiKey});
|
14 |
if (!keyDoc) {
|
15 |
return res.status(403).json({ error: 'Invalid API Key' });
|
16 |
}
|
|
|
23 |
const apiLimiter = rateLimit({
|
24 |
windowMs: 15 * 60 * 1000,
|
25 |
max: 100,
|
26 |
+
keyGenerator: (req) => req.headers['x-api-key'],
|
27 |
message: 'Too many requests from this API Key'
|
28 |
});
|
29 |
|
30 |
class CheckMilWare {
|
31 |
constructor() {
|
32 |
+
this.dbClient = new Database("AkenoXJs");
|
33 |
}
|
34 |
|
35 |
async handle(req, res, next) {
|