Bird
0
0

Consider this guard implementation:

medium📝 Predict Output Q5 of 15
NestJS - Guards
Consider this guard implementation:
async canActivate(context: ExecutionContext) {
  const user = await this.authService.validateUser(context);
  return user !== null;
}

What will happen if validateUser returns null?
AAccess is denied because canActivate returns false
BAccess is granted because null is truthy
CAn exception is thrown automatically
DThe guard retries validation
Step-by-Step Solution
Solution:
  1. Step 1: Understand the return condition

    canActivate returns true only if user is not null; null means false.
  2. Step 2: Effect on access

    Returning false denies access.
  3. Final Answer:

    Access is denied because canActivate returns false -> Option A
  4. Quick Check:

    Null user means false = D [OK]
Quick Trick: Null user means deny access [OK]
Common Mistakes:
  • Thinking null is truthy
  • Expecting automatic exceptions
  • Assuming guard retries validation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes