The map is a static object defined on the root `Route` class. This means that any changes made by its derived classes (e.g. by specifying the `@authenticate` annotation on some methods) will impact the object for all the other derived classes too. To prevent clashes, the structure of the map has been changed from: ``` method -> preRequestHandler ``` to: ``` RouteClass.name -> method -> preRequestHandler ```
This commit is contained in:
parent
595d9528d5
commit
d49f8ec013
2 changed files with 9 additions and 4 deletions
|
@ -18,7 +18,11 @@ class AuthInfo {
|
|||
function authenticate(roles: RoleName[] = []) {
|
||||
return function (route: any, method: string) {
|
||||
const routeClass = (<typeof Route> route.constructor);
|
||||
routeClass.preRequestHandlers[method] = async (req: Request): Promise<AuthInfo> => {
|
||||
if (!routeClass.preRequestHandlers[routeClass.name]) {
|
||||
routeClass.preRequestHandlers[routeClass.name] = {}
|
||||
}
|
||||
|
||||
routeClass.preRequestHandlers[routeClass.name][method] = async (req: Request): Promise<AuthInfo> => {
|
||||
let user: Optional<User>;
|
||||
let session: Optional<UserSession>;
|
||||
|
||||
|
|
|
@ -14,8 +14,8 @@ import { logRequest } from '../helpers/logging';
|
|||
|
||||
abstract class Route {
|
||||
protected readonly path: string;
|
||||
// Method -> Handler mapping
|
||||
public static preRequestHandlers: Record<string, RequestHandler> = {};
|
||||
// Route -> Method -> Handler mapping
|
||||
public static preRequestHandlers: Record<string, Record<string, RequestHandler>> = {};
|
||||
|
||||
constructor(path: string) {
|
||||
this.path = path;
|
||||
|
@ -76,9 +76,10 @@ abstract class Route {
|
|||
logRequest(req);
|
||||
|
||||
try {
|
||||
const routeClass = <typeof Route> this.constructor;
|
||||
// @ts-expect-error
|
||||
const handler = (this[handlerName]) as ((req: Request, res: Response, auth: AuthInfo) => Promise<void>);
|
||||
const preRequestHandler = (<typeof Route> this.constructor).preRequestHandlers[handlerName];
|
||||
const preRequestHandler = routeClass.preRequestHandlers[routeClass.name]?.[handlerName];
|
||||
|
||||
let authInfo: Optional<AuthInfo>
|
||||
if (preRequestHandler) {
|
||||
|
|
Loading…
Add table
Reference in a new issue