-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.mjs
More file actions
40 lines (34 loc) · 845 Bytes
/
utils.mjs
File metadata and controls
40 lines (34 loc) · 845 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
29
30
31
32
33
34
35
36
37
38
39
40
import jwt from "jsonwebtoken";
import * as dotenv from "dotenv";
dotenv.config();
/**
* Checks if the jwt token matched the :id param
*
* @param {*} req
* @param {*} res
* @param {*} next
* @returns
*/
export const authenticateClientId = (req, res, next) => {
// Allow if the param id is not given
if (!req.params["id"]) next();
// Check the cookie
const cookieToken = req.cookies.__userToken;
// Try validating the jwt token
try {
jwt.verify(cookieToken, process.env.JWT_SECRET);
} catch (err) {
return res.json({
access: "denied",
description: "Invalid or missing jwt token",
});
}
const decodedToken = jwt.decode(cookieToken);
if (decodedToken.client_id !== req.params["id"]) {
return res.json({
access: "denied",
description: `Missing permission to view ${req.params["id"]}`,
});
}
next();
};