Guards in NestJS control whether a request can proceed to the route handler by checking conditions like authentication or roles.
Click to reveal answer
beginner
How do you apply multiple guards to a single route in NestJS?
You apply multiple guards by passing them as arguments to the @UseGuards() decorator, like @UseGuards(Guard1, Guard2).
Click to reveal answer
intermediate
What happens if one guard in a combined set returns false?
If any guard returns false, NestJS stops and denies access to the route, so all guards must pass for the request to proceed.
Click to reveal answer
intermediate
Can you combine guards globally and locally in NestJS?
Yes, you can apply guards globally for all routes and also locally on specific controllers or routes. Local guards run after global ones.
Click to reveal answer
beginner
Example: How to combine AuthGuard and RolesGuard on a route?
Use @UseGuards(AuthGuard, RolesGuard) above the route handler. This checks authentication first, then user roles before allowing access.
Click to reveal answer
How do you apply multiple guards to a NestJS route?
ANestJS does not support multiple guards
BPass them as separate @UseGuards() decorators
CUse @ApplyGuards() decorator
DPass them as an array to a single @UseGuards() decorator
✗ Incorrect
You pass multiple guards as arguments to a single @UseGuards() decorator, like @UseGuards(Guard1, Guard2).
What happens if one guard returns false in a combined guard set?
AAccess is denied and request stops
BOnly that guard is ignored
CThe request proceeds anyway
DNestJS throws an error
✗ Incorrect
If any guard returns false, NestJS denies access and stops processing the request.
Can you combine global and local guards in NestJS?
ANo, only one type can be used
BYes, global guards run before local guards
CYes, local guards run before global guards
DOnly global guards are supported
✗ Incorrect
Global guards run first, then local guards run on specific controllers or routes.
Which decorator is used to apply guards in NestJS?
A@UseFilters()
B@UseInterceptors()
C@UseGuards()
D@UsePipes()
✗ Incorrect
@UseGuards() is the decorator to apply guards to routes or controllers.
What is a common use case for combining AuthGuard and RolesGuard?
ATo check user authentication and authorization
BTo validate request body format
CTo log requests
DTo handle exceptions
✗ Incorrect
AuthGuard checks if the user is logged in, RolesGuard checks if the user has the right permissions.
Explain how NestJS processes multiple guards applied to a route.
Think about a security checkpoint where multiple checks must pass.
You got /3 concepts.
Describe how to apply guards globally and locally in NestJS and how they interact.
Consider a building with a main gate (global) and room doors (local).
You got /3 concepts.
Practice
(1/5)
1.
What happens when you use @UseGuards(AuthGuard, RolesGuard) on a NestJS route handler?
Choose the best description.
easy
A. Only AuthGuard is checked; RolesGuard is ignored.
B. Both AuthGuard and RolesGuard must allow access for the request to proceed.
C. Either AuthGuard or RolesGuard allowing access is enough to proceed.
D. Guards run in parallel but only the first to approve grants access.
Solution
Step 1: Understand how multiple guards work in NestJS
When multiple guards are used with @UseGuards(), NestJS runs them in sequence and requires all to approve access.
Step 2: Apply this to the given guards
Both AuthGuard and RolesGuard must return true for the request to proceed.
Final Answer:
Both AuthGuard and RolesGuard must allow access for the request to proceed. -> Option B
Quick Check:
All guards must approve = A [OK]
Hint: All guards must pass for access, not just one [OK]
Common Mistakes:
Thinking only one guard needs to pass
Assuming guards run in parallel and race
Believing guards are ignored if one fails
2.
Which of the following is the correct syntax to apply two guards AuthGuard and RolesGuard on a controller method?
?
easy
A. @UseGuards(AuthGuard && RolesGuard)
handleRequest() {}
B. @UseGuards(AuthGuard RolesGuard)
handleRequest() {}
C. @UseGuards(AuthGuard || RolesGuard)
handleRequest() {}
D. @UseGuards(AuthGuard, RolesGuard)
handleRequest() {}
Solution
Step 1: Recall the correct syntax for multiple guards
In NestJS, multiple guards are passed as separate arguments to @UseGuards(), separated by commas.
Step 2: Check each option
@UseGuards(AuthGuard, RolesGuard)
handleRequest() {} correctly uses comma-separated guards. @UseGuards(AuthGuard RolesGuard)
handleRequest() {} misses the comma between guards, causing a syntax error. Using && or || evaluates to only one guard.
Final Answer:
@UseGuards(AuthGuard, RolesGuard)
handleRequest() {} -> Option D
Quick Check:
Comma-separated guards = C [OK]
Hint: Use commas to separate guards, avoid logical operators or missing commas in @UseGuards() [OK]
Common Mistakes:
Omitting comma between guards
Using && or || inside @UseGuards()
Forgetting to separate guards by commas
3.
Given the following code snippet, what will be the output when a request is made?
The guards inside @UseGuards() must be separated by commas.
Step 2: Identify the error in the snippet
The snippet misses a comma between AuthGuard and RolesGuard, causing a syntax error.
Final Answer:
Missing comma between guards in @UseGuards() -> Option C
Quick Check:
Comma missing between guards = D [OK]
Hint: Check commas between guards inside @UseGuards() [OK]
Common Mistakes:
Forgetting commas between guards
Passing guards as an array incorrectly
Misplacing decorators order
5.
You want to protect a route so that only authenticated users with the role 'admin' can access it. You have AuthGuard and RolesGuard available. How should you combine them in your controller method?
hard
A. @UseGuards(AuthGuard, RolesGuard) // RolesGuard checks for 'admin' role
B. @UseGuards(RolesGuard, AuthGuard) // Order does not matter
C. @UseGuards(AuthGuard || RolesGuard) // Either guard allows access
D. @UseGuards([RolesGuard, AuthGuard]) // Guards passed as array
Solution
Step 1: Understand guard order and combination
Guards are combined by passing them comma-separated in @UseGuards(). Order matters: AuthGuard first authenticates and sets req.user, then RolesGuard checks the role.
Step 2: Choose correct syntax and logic
@UseGuards(AuthGuard, RolesGuard) // RolesGuard checks for 'admin' role uses correct syntax, order, and notes the role check. Reversing to RolesGuard first fails as no user is set, and claims order irrelevant which is misleading. Array with reversed order fails similarly. Logical operators pass only one guard.
Final Answer:
@UseGuards(AuthGuard, RolesGuard) // RolesGuard checks for 'admin' role -> Option A
Quick Check:
Comma-separated guards with role check = B [OK]
Hint: Use comma-separated guards; RolesGuard checks roles [OK]
Common Mistakes:
Using logical operators inside @UseGuards()
Passing guards as an array
Assuming guard order always matters
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.