• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: how to check userroles in each controller in nestjs

Resolved: how to check userroles in each controller in nestjs

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

There was no answer to the question I left before, so I’m writing again.
Please understand.
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!

Answer:

NestJS authentication provides different mechanisms like:
  1. Extending guards
  2. Enable authentication globally

For your specific use case, with the first mechanism, you can extend your auth guard to check if the user has a specific role.
Note that if you expect the different controllers to be accessed with different user roles then you have to define the specific roles guard in the controllers themselves.

If you have better answer, please add a comment about this, thank you!

nestjs
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How can I copy files using the ansible.builtin.copy module and avoid conflicting file names?

26/03/2023

Resolved: Reshape tensors of unknown shape with tf.function

26/03/2023

Resolved: Use Svelte Component as Slot

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.