Bird
0
0

Consider this guard implementation:

medium📝 Predict Output Q4 of 15
NestJS - Guards
Consider this guard implementation:
canActivate(context: ExecutionContext) {
  const request = context.switchToHttp().getRequest();
  return request.headers['authorization'] === 'Bearer token123';
}

What will be the result if the 'authorization' header is not present in the request?
AThe guard retries fetching the header
BThe guard throws an exception due to undefined header
CThe guard returns true, allowing access
DThe guard returns false, denying access
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the guard logic

    The guard checks if the 'authorization' header equals a specific string.
  2. Step 2: Consider missing header scenario

    If the header is missing, request.headers['authorization'] is undefined, so the comparison returns false.
  3. Final Answer:

    The guard returns false, denying access -> Option D
  4. Quick Check:

    Missing header means condition fails, so access denied. [OK]
Quick Trick: Missing header causes guard to deny access [OK]
Common Mistakes:
  • Assuming missing headers cause exceptions
  • Thinking guard returns true by default

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes