Auth: fix middleware applied globally and explicitly log them

This commit is contained in:
Alice Gaudon 2021-01-21 15:44:30 +01:00
parent 2c66c66a39
commit 3e0a25874e
3 changed files with 7 additions and 6 deletions

View File

@ -251,7 +251,7 @@ export default abstract class Application implements Extendable<ApplicationCompo
initRouter.use(controller.getRoutesPrefix(), fileUploadFormRouter); initRouter.use(controller.getRoutesPrefix(), fileUploadFormRouter);
handleRouter.use(controller.getRoutesPrefix(), mainRouter); handleRouter.use(controller.getRoutesPrefix(), mainRouter);
log.info(`> Registered routes for controller ${controller.constructor.name}`); log.info(`> Registered routes for controller ${controller.constructor.name} at ${controller.getRoutesPrefix()}`);
} }
handleRouter.use((req: Request) => { handleRouter.use((req: Request) => {

View File

@ -75,6 +75,7 @@ export default abstract class Controller {
protected use(handler: RequestHandler): void { protected use(handler: RequestHandler): void {
this.router.use(this.wrap(handler)); this.router.use(this.wrap(handler));
log.info('Installed anonymous middleware on ' + this.getRoutesPrefix());
} }
protected useMiddleware(...middlewares: MiddlewareType<Middleware>[]): void { protected useMiddleware(...middlewares: MiddlewareType<Middleware>[]): void {
@ -85,6 +86,8 @@ export default abstract class Controller {
} else { } else {
this.router.use(this.wrap(instance.getRequestHandler())); this.router.use(this.wrap(instance.getRequestHandler()));
} }
log.info('Installed ' + middleware.name + ' on ' + this.getRoutesPrefix());
} }
} }

View File

@ -24,11 +24,9 @@ export default class AuthController extends Controller {
next(); next();
}); });
this.useMiddleware(RequireGuestMiddleware); this.get('/', this.getAuth, 'auth', RequireGuestMiddleware);
this.post('/login', this.postLogin, 'login', RequireGuestMiddleware);
this.get('/', this.getAuth, 'auth'); this.post('/register', this.postRegister, 'register', RequireGuestMiddleware);
this.post('/login', this.postLogin, 'login');
this.post('/register', this.postRegister, 'register');
} }
protected async getAuth(req: Request, res: Response, _next: NextFunction): Promise<void> { protected async getAuth(req: Request, res: Response, _next: NextFunction): Promise<void> {