Controller: add useMiddleware method
This commit is contained in:
parent
bdaf815aec
commit
4db7217876
@ -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,
|
||||
|
@ -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> {
|
||||
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user