blob: a1ba4982af0e595e1246d96c6c80df25cebde394 (
plain) (
blame)
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 { validateJwtToken } from '#util/jwtUtils.js';
import { DbUser } from '#db/schemas/index.js';
/**
* @param options {AuthValidationOptions}
* @returns {(function(*, *, *): void)|*}
*/
export function validateAuth(options) {
return async function (req, res, next) {
const auth = (req.auth = validateJwtToken(req.headers.authorization));
if (!auth) {
res.status(401).send('Unauthorized');
return;
}
const user = (req.user = await DbUser.findById(auth.id).exec());
if (options.roles && !options.roles.includes(user.type)) {
return;
}
next();
};
}
class AuthValidationOptions {
roles;
}
|