Bird
0
0

How can you combine a guard with a custom decorator to restrict access based on user permissions in NestJS?

hard📝 Application Q9 of 15
NestJS - Guards
How can you combine a guard with a custom decorator to restrict access based on user permissions in NestJS?
AApply the guard only on the decorator function
BCreate a decorator to set metadata, then read it in the guard via Reflector
CUse the decorator to throw errors directly without a guard
DUse the decorator to modify the request object directly
Step-by-Step Solution
Solution:
  1. Step 1: Understand metadata usage

    Custom decorators set metadata on routes or controllers.
  2. Step 2: Guard reads metadata

    Guards use Reflector to read metadata and decide access.
  3. Step 3: Eliminate incorrect options

    Throwing errors in decorators or modifying requests directly is not standard; guards control access.
  4. Final Answer:

    Create a decorator to set metadata, then read it in the guard via Reflector -> Option B
  5. Quick Check:

    Decorator sets metadata, guard reads it = C [OK]
Quick Trick: Use Reflector in guards to read decorator metadata [OK]
Common Mistakes:
  • Throwing errors in decorators
  • Modifying requests in decorators
  • Applying guards incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes