Bird
0
0

Consider this NestJS route guarded by AuthGuard and RolesGuard:

medium📝 component behavior Q4 of 15
NestJS - Guards

Consider this NestJS route guarded by AuthGuard and RolesGuard:

@UseGuards(AuthGuard, RolesGuard)
@Get('dashboard')
dashboard() {
  return 'Dashboard Data';
}

What happens if AuthGuard allows the request but RolesGuard denies it?

AThe request is denied and an authorization error is returned.
BThe request proceeds and returns 'Dashboard Data'.
COnly <code>AuthGuard</code> result matters; the request proceeds.
DNestJS throws a runtime error due to conflicting guards.
Step-by-Step Solution
Solution:
  1. Step 1: Understand guard behavior

    All guards applied via @UseGuards must return true for the request to proceed.
  2. Step 2: Analyze the scenario

    Since RolesGuard denies access, it returns false or throws an exception, blocking the request.
  3. Final Answer:

    The request is denied and an authorization error is returned. -> Option A
  4. Quick Check:

    All guards must pass for access [OK]
Quick Trick: All guards must approve for access to proceed [OK]
Common Mistakes:
  • Assuming only the first guard's result matters
  • Thinking guards combine with OR logic
  • Believing NestJS throws errors on guard conflicts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes