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));
|
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(
|
protected get(
|
||||||
path: PathParams,
|
path: PathParams,
|
||||||
handler: RequestHandler,
|
handler: RequestHandler,
|
||||||
|
@ -24,9 +24,11 @@ export default class AuthController extends Controller {
|
|||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
this.get('/', this.getAuth, 'auth', RequireGuestMiddleware);
|
this.useMiddleware(RequireGuestMiddleware);
|
||||||
this.post('/login', this.postLogin, 'login', RequireGuestMiddleware);
|
|
||||||
this.post('/register', this.postRegister, 'register', 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> {
|
protected async getAuth(req: Request, res: Response, _next: NextFunction): Promise<void> {
|
||||||
|
@ -38,14 +38,13 @@ export default class BackendController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public routes(): void {
|
public routes(): void {
|
||||||
this.get('/', this.getIndex, 'backend', RequireAuthMiddleware, RequireAdminMiddleware);
|
this.useMiddleware(RequireAuthMiddleware, RequireAdminMiddleware);
|
||||||
|
|
||||||
|
this.get('/', this.getIndex, 'backend');
|
||||||
if (User.isApprovalMode()) {
|
if (User.isApprovalMode()) {
|
||||||
this.get('/accounts-approval', this.getAccountApproval, 'accounts-approval',
|
this.get('/accounts-approval', this.getAccountApproval, 'accounts-approval');
|
||||||
RequireAuthMiddleware, RequireAdminMiddleware);
|
this.post('/accounts-approval/approve', this.postApproveAccount, 'approve-account');
|
||||||
this.post('/accounts-approval/approve', this.postApproveAccount, 'approve-account',
|
this.post('/accounts-approval/reject', this.postRejectAccount, 'reject-account');
|
||||||
RequireAuthMiddleware, RequireAdminMiddleware);
|
|
||||||
this.post('/accounts-approval/reject', this.postRejectAccount, 'reject-account',
|
|
||||||
RequireAuthMiddleware, RequireAdminMiddleware);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user