Bird
0
0

You want to create a role-based guard that allows access if the user has any one of multiple roles specified on a route. Which approach correctly implements this behavior?

hard📝 Application Q15 of 15
NestJS - Guards
You want to create a role-based guard that allows access if the user has any one of multiple roles specified on a route. Which approach correctly implements this behavior?
AUse multiple guards each checking one role and combine them with @UseGuards
BCheck only the first role in roles array against user.role in canActivate
CUse @Roles('admin', 'editor') decorator and check if user.role is included in roles array inside canActivate
DIgnore roles and allow access to all authenticated users
Step-by-Step Solution
Solution:
  1. Step 1: Understand multiple roles in @Roles decorator

    @Roles can accept multiple roles as arguments to specify allowed roles.
  2. Step 2: Implement canActivate to check inclusion

    Checking if user.role is in roles array allows access if user has any allowed role.
  3. Final Answer:

    Use @Roles('admin', 'editor') decorator and check if user.role is included in roles array inside canActivate -> Option C
  4. Quick Check:

    Check user role in roles array for multiple allowed roles [OK]
Quick Trick: Check if user role is in roles array for multiple roles [OK]
Common Mistakes:
  • Checking only first role ignores others
  • Using multiple guards unnecessarily
  • Allowing all authenticated users ignores roles

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes