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 | 39x 855x 901x 901x 34x 34x 867x 867x 39x 612x 612x | import { Request, Response, NextFunction } from "express";
import { getAuthorizedUsername } from "server/utils/authHeader";
import { UserGroup } from "shared/types/models/user";
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;
};
|