-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmiddleware.ts
More file actions
28 lines (23 loc) · 811 Bytes
/
middleware.ts
File metadata and controls
28 lines (23 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { BAD_REQUEST, UNAUTHORISED } from "../config/strings";
import { getApiKeyUsingKeyId } from "./queries";
import { getUser } from "../user/queries";
import { Apikey } from "@medialit/models";
import logger from "../services/log";
export default async function apikey(
req: any,
res: any,
next: (...args: any[]) => void,
) {
const reqKey = req.body?.apikey || req.headers["x-medialit-apikey"];
if (!reqKey) {
logger.error({}, "API key is missing");
return res.status(401).json({ error: UNAUTHORISED });
}
const apiKey: Apikey | null = await getApiKeyUsingKeyId(reqKey);
if (!apiKey) {
return res.status(401).json({ error: UNAUTHORISED });
}
req.user = await getUser(apiKey!.userId.toString());
req.apikey = apiKey.key;
next();
}