Bird
0
0

Identify the issue in this guard:

medium📝 Debug Q6 of 15
NestJS - Authentication
Identify the issue in this guard:
export class AdminGuard implements CanActivate { canActivate(context: ExecutionContext) { return context.switchToHttp().getRequest().user.isAdmin; } }
AIt may throw an error if user is undefined
BIt always returns true regardless of user role
CIt does not implement CanActivate interface
DIt uses an incorrect method to get the request
Step-by-Step Solution
Solution:
  1. Step 1: Check user property access

    If user is undefined, accessing isAdmin causes an error.
  2. Step 2: Guard should handle missing user safely

    Proper null checks or default values are needed.
  3. Final Answer:

    It may throw an error if user is undefined -> Option A
  4. Quick Check:

    Check for undefined user before accessing properties [OK]
Quick Trick: Always verify user exists before property access [OK]
Common Mistakes:
  • Assuming user is always defined
  • Ignoring potential runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes