C. Using getHandler() instead of getClass() for roles
D. Not checking if user exists before accessing user.role
Solution
Step 1: Review user role access
The code accesses user.role without verifying if user is defined.
Step 2: Identify potential runtime error
If user is undefined, accessing user.role causes an error.
Final Answer:
Not checking if user exists before accessing user.role -> Option D
Quick Check:
Always check user before role access [OK]
Hint: Check user object exists before role check [OK]
Common Mistakes:
Assuming user is always present
Confusing getHandler() and getClass() usage
Thinking async is required for synchronous guard
5. How can you combine role-based authorization with route-level guards to allow access only if the user has either 'admin' or 'manager' role in NestJS?
hard
A. Use @Roles('admin', 'manager') decorator and a RolesGuard that checks for any matching role
B. Use two separate @Roles decorators, one for 'admin' and one for 'manager', on the same route
C. Use @UseGuards(AdminGuard, ManagerGuard) without @Roles decorator
D. Check roles manually inside the controller method without decorators or guards
Solution
Step 1: Understand role decorator usage
@Roles() accepts multiple roles to allow any of them access.
Step 2: Use a guard that checks if user role matches any allowed role
The RolesGuard reads roles from metadata and checks if user role is included.
Final Answer:
Use @Roles('admin', 'manager') decorator and a RolesGuard that checks for any matching role -> Option A
Quick Check:
Multiple roles in @Roles() + guard = flexible access [OK]
Hint: Pass multiple roles in one @Roles() decorator [OK]
Common Mistakes:
Using multiple @Roles decorators on one route
Applying multiple guards instead of one flexible guard
Skipping decorators and guards for manual checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.