Total Complexity | 5 |
Complexity/F | 5 |
Lines of Code | 38 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 100% |
Changes | 0 |
1 | // eslint-disable-next-line no-unused-vars |
||
2 | import express from "express"; |
||
3 | import apiKeyModel from "../models/api-key.js"; |
||
4 | |||
5 | /** |
||
6 | * |
||
7 | * @param {express.Request} req |
||
8 | * @param {express.Response} res |
||
9 | * @param {express.NextFunction} next |
||
10 | */ |
||
11 | async function apiKeyHandler(req, res, next) { |
||
12 | 100 | if (req.path === "/admin/feed") { |
|
13 | 2 | return next(); |
|
14 | } |
||
15 | |||
16 | 98 | const apiKey = req.headers['x-api-key']; |
|
17 | |||
18 | 98 | const apiKeyString = Array.isArray(apiKey) ? apiKey[0] : apiKey; |
|
19 | |||
20 | 98 | if (!apiKeyString) { |
|
21 | 1 | return res.status(401).json({ |
|
22 | success: false, |
||
23 | message: 'API key is required.' |
||
24 | }); |
||
25 | } |
||
26 | |||
27 | 97 | const isValidKey = await apiKeyModel.checkOne(apiKeyString); |
|
28 | |||
29 | 97 | if (!isValidKey) { |
|
30 | 1 | return res.status(401).json({ |
|
31 | success: false, |
||
32 | message: 'Invalid or missing API key. Access denied.' |
||
33 | }); |
||
34 | } |
||
35 | |||
36 | 96 | return next(); |
|
37 | } |
||
38 | |||
39 | export default apiKeyHandler; |
||
40 |