Controller: add useMiddleware method

This commit is contained in:
Alice Gaudon 2020-12-30 14:10:58 +01:00
parent bdaf815aec
commit 4db7217876
3 changed files with 22 additions and 10 deletions

View File

@ -77,6 +77,17 @@ export default abstract class Controller {
this.router.use(this.wrap(handler));
}
protected useMiddleware(...middlewares: MiddlewareType<Middleware>[]): void {
for (const middleware of middlewares) {
const instance = new middleware(this.getApp());
if (instance instanceof FileUploadMiddleware) {
this.fileUploadFormRouter.use(this.wrap(instance.getRequestHandler()));
} else {
this.router.use(this.wrap(instance.getRequestHandler()));
}
}
}
protected get(
path: PathParams,
handler: RequestHandler,

View File

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

View File

@ -38,14 +38,13 @@ export default class BackendController extends Controller {
}
public routes(): void {
this.get('/', this.getIndex, 'backend', RequireAuthMiddleware, RequireAdminMiddleware);
this.useMiddleware(RequireAuthMiddleware, RequireAdminMiddleware);
this.get('/', this.getIndex, 'backend');
if (User.isApprovalMode()) {
this.get('/accounts-approval', this.getAccountApproval, 'accounts-approval',
RequireAuthMiddleware, RequireAdminMiddleware);
this.post('/accounts-approval/approve', this.postApproveAccount, 'approve-account',
RequireAuthMiddleware, RequireAdminMiddleware);
this.post('/accounts-approval/reject', this.postRejectAccount, 'reject-account',
RequireAuthMiddleware, RequireAdminMiddleware);
this.get('/accounts-approval', this.getAccountApproval, 'accounts-approval');
this.post('/accounts-approval/approve', this.postApproveAccount, 'approve-account');
this.post('/accounts-approval/reject', this.postRejectAccount, 'reject-account');
}
}