Bird
0
0

How can you combine multiple guards so that RolesGuard runs only if AuthGuard passes?

hard📝 Application Q9 of 15
NestJS - Guards

How can you combine multiple guards so that RolesGuard runs only if AuthGuard passes?

AApply guards separately on different controllers
BUse <code>@UseGuards(AuthGuard, RolesGuard)</code> because guards run sequentially and stop on failure
CCombine guards with logical AND inside a single custom guard
DUse <code>@UseGuards(RolesGuard, AuthGuard)</code> to run RolesGuard first
Step-by-Step Solution
Solution:
  1. Step 1: Understand guard execution order

    Guards run in the order they are listed in @UseGuards.
  2. Step 2: Analyze sequential guard behavior

    If AuthGuard fails, RolesGuard does not run. If AuthGuard passes, RolesGuard runs next.
  3. Final Answer:

    Use @UseGuards(AuthGuard, RolesGuard) because guards run sequentially and stop on failure -> Option B
  4. Quick Check:

    Guards run in order, stop on failure [OK]
Quick Trick: Order matters; guards stop running on first failure [OK]
Common Mistakes:
  • Reversing guard order
  • Assuming all guards run regardless of failures
  • Trying to combine guards with logical operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes