Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parametrizable middleware #61

Closed
AurelieV opened this issue May 13, 2017 · 3 comments
Closed

Parametrizable middleware #61

AurelieV opened this issue May 13, 2017 · 3 comments

Comments

@AurelieV
Copy link

In my express applications, I often define a role check middleware factory.
It takes a role array as a parameter, so I can reuse it for different roles checking (admin, moderator, etc) without rewrite a specific middleware for each case.

Is it possible to do something similar with Nest middlewares?

@kamilmysliwiec
Copy link
Member

Hi @AurelieV,
You can use MiddlewaresConsumer.

export class UsersModule implements NestModule {
    configure(consumer: MiddlewaresConsumer) {
        consumer.apply(AuthMiddleware).with(['admin', 'user']).forRoutes(UsersController);
    }
}

Then in the middleware:

resolve(roles: string[]) {
    return (req, res, next) => {
        ///
        next();
    };
}

@AurelieV
Copy link
Author

Thanks, I was searching this on the docs :)

@lock
Copy link

lock bot commented Sep 25, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants