What will happen if a user without isAdmin tries to access the guarded route?
medium
A. Access will be granted because the guard always returns true
B. The guard will ignore the user and allow access
C. An error will be thrown due to missing isAdmin property
D. Access will be denied because isAdmin is false or missing
Solution
Step 1: Analyze the guard condition
The guard returns true only if request.user exists and request.user.isAdmin is truthy.
Step 2: Consider user without isAdmin
If isAdmin is false or missing, the condition returns false, denying access.
Final Answer:
Access will be denied because isAdmin is false or missing -> Option D
Quick Check:
Guard returns false if not admin = B [OK]
Hint: 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
4. Identify the error in this guard implementation:
canActivate(context: ExecutionContext) {
const request = context.getRequest();
if (!request.user) {
throw new Error('User not found');
}
return true;
}
medium
A. Throwing an error inside a guard instead of returning false
B. Missing async keyword for canActivate method
C. Using context.getRequest() instead of context.switchToHttp().getRequest()
D. Returning true without checking user roles
Solution
Step 1: Check method usage for request
While context.getRequest() is invalid, the main issue is guard behavior on failure.
Step 2: Understand guard error handling
Guards should return false to deny access, not throw errors which cause server crashes.
Final Answer:
Throwing an error inside a guard instead of returning false -> Option A
Quick Check:
Guards return false, not throw errors [OK]
Hint: Return false to block access, don't throw errors in guards [OK]
Common Mistakes:
Throwing errors instead of returning false
Using incorrect context method for request
Assuming async is required without async code
5. You want to restrict access to a route only to logged-in users with the role 'admin' or 'moderator'. Which guard logic correctly implements this?
hard
A. Return true if request.user does not exist
B. Return true if request.user exists and request.user.role === 'admin' && request.user.role === 'moderator'
C. Return true if request.user exists and request.user.role === 'admin' || request.user.role === 'moderator'
D. Return true if request.user exists and request.user.role !== 'admin' || request.user.role !== 'moderator'
Solution
Step 1: Understand role conditions
The user must be logged in and have role 'admin' OR 'moderator' to access.
Step 2: Evaluate logical operators
Return true if request.user exists and request.user.role === 'admin' || request.user.role === 'moderator' uses OR (||) correctly. Return true if request.user exists and request.user.role === 'admin' && request.user.role === 'moderator' uses AND (&&) which is impossible for one role. Return true if request.user exists and request.user.role !== 'admin' || request.user.role !== 'moderator' uses negations incorrectly. Return true if request.user does not exist allows access without user.
Final Answer:
Return true if request.user exists and request.user.role === 'admin' || request.user.role === 'moderator' -> Option C
Quick Check:
Use OR for multiple allowed roles = D [OK]
Hint: Use OR (||) to allow multiple roles [OK]
Common Mistakes:
Using AND (&&) instead of OR (||) for roles
Allowing access when user is missing
Incorrect negation logic in conditions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.