Search code examples
nestjsroles

how to check user role on nestjs


I am making a project using nestjs.

I want to prevent users with a specific role in all controllers except for one controller.

I know, add @UseGuard(RoleGuard) @Role(UserRole.Guest) to each controller.

But I have a lot of controllers, and more will be added over and over again.

Is there a way to do it all at once like middleware?

thank you!


Solution

  • Could you not have a base controller that you extend and add the decorator to the constructor or something?

    Also, check out useGlobalPipes to see if that can help with shared request logic.