Bird
0
0

Given this guard code snippet:

medium📝 component behavior Q13 of 15
NestJS - Guards
Given this guard code snippet:
canActivate(context: ExecutionContext) {
  const request = context.switchToHttp().getRequest();
  return request.user && request.user.isAdmin;
}

What will happen if a user without isAdmin tries to access the guarded route?
AAccess will be granted because the guard always returns true
BThe guard will ignore the user and allow access
CAn error will be thrown due to missing <code>isAdmin</code> property
DAccess will be denied because <code>isAdmin</code> is false or missing
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the guard condition

    The guard returns true only if request.user exists and request.user.isAdmin is truthy.
  2. Step 2: Consider user without isAdmin

    If isAdmin is false or missing, the condition returns false, denying access.
  3. Final Answer:

    Access will be denied because isAdmin is false or missing -> Option D
  4. Quick Check:

    Guard returns false if not admin = B [OK]
Quick Trick: Guard returns true only if user is admin [OK]
Common Mistakes:
  • Assuming missing property causes error
  • Thinking guard always returns true
  • Ignoring the boolean check on isAdmin

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes