Bird
0
0

You want to apply AuthGuard globally but RolesGuard only on some routes. How do you combine guards correctly?

hard📝 Conceptual Q8 of 15
NestJS - Guards

You want to apply AuthGuard globally but RolesGuard only on some routes. How do you combine guards correctly?

AApply <code>AuthGuard</code> in main.ts globally, and use <code>@UseGuards(RolesGuard)</code> on specific routes
BApply both guards globally in main.ts
CApply both guards on every route with <code>@UseGuards(AuthGuard, RolesGuard)</code>
DApply <code>RolesGuard</code> globally and <code>AuthGuard</code> on routes
Step-by-Step Solution
Solution:
  1. Step 1: Understand global guard application

    Global guards apply to all routes automatically.
  2. Step 2: Combine global and route-specific guards

    Apply AuthGuard globally, then add RolesGuard only where needed with @UseGuards.
  3. Final Answer:

    Apply AuthGuard in main.ts globally, and use @UseGuards(RolesGuard) on specific routes -> Option A
  4. Quick Check:

    Global + route guards = global + @UseGuards [OK]
Quick Trick: Global guards apply everywhere; add others per route [OK]
Common Mistakes:
  • Applying all guards globally unnecessarily
  • Applying guards only on routes without global guard
  • Confusing order of guard application

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes