Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 42x 930x 1199x 1199x 34x 34x 1165x 1165x 42x 835x 835x | import { NextFunction, Request, Response } from "express";
import { UserGroup } from "shared/types/models/user";
import { getAuthorizedUsername } from "server/utils/authHeader";
declare module "express-serve-static-core" {
interface Request {
auth?: { username: string };
}
}
export const requireAuth =
(allowedGroups: UserGroup | UserGroup[]) =>
(req: Request, res: Response, next: NextFunction): void => {
const username = getAuthorizedUsername(
req.headers.authorization,
allowedGroups,
);
if (!username) {
res.sendStatus(401);
return;
}
req.auth = { username };
next();
};
export const getAuthUsername = (req: {
auth?: { username: string };
}): string => {
Iif (!req.auth) {
// eslint-disable-next-line no-restricted-syntax -- programming error if requireAuth was not wired
throw new Error(
"requireAuth middleware did not run before this handler — wire requireAuth() in apiRoutes",
);
}
return req.auth.username;
};
|