A role-based guard in NestJS is a piece of code that checks if a user has the right role to access a certain part of the application. It helps protect routes by allowing only users with specific roles to proceed.
Click to reveal answer
beginner
How do you apply a role-based guard to a route in NestJS?
You apply a role-based guard by using the @UseGuards() decorator on a controller or route handler and passing the guard class that checks user roles.
Click to reveal answer
intermediate
What is the purpose of the CanActivate interface in NestJS guards?
The CanActivate interface defines a method called canActivate() that returns true or false to allow or deny access. Role-based guards implement this interface to check user roles before allowing access.
Click to reveal answer
intermediate
How can you pass roles to a role-based guard in NestJS?
You can pass roles using a custom decorator that stores roles as metadata on the route. The guard then reads this metadata to check if the user has one of the required roles.
Click to reveal answer
beginner
Why is role-based guarding important in web applications?
Role-based guarding helps keep parts of an app safe by making sure only users with the right permissions can access sensitive data or actions. It supports security and proper user management.
Click to reveal answer
Which NestJS interface must a role-based guard implement?
APipeTransform
BOnModuleInit
CExceptionFilter
DCanActivate
✗ Incorrect
Role-based guards implement the CanActivate interface to decide if a request can proceed.
How do you specify which roles can access a route?
ABy setting roles in the main.ts file
BBy hardcoding roles inside the guard only
CBy using a custom decorator that sets roles metadata
DBy using the @Injectable decorator
✗ Incorrect
A custom decorator sets roles as metadata on routes, which the guard reads to check access.
What does the canActivate() method return to allow access?
Atrue
Bnull
Cundefined
Dfalse
✗ Incorrect
Returning true from canActivate() means access is allowed.
Where do you apply a role-based guard in NestJS?
AIn the database schema
BOn controllers or route handlers using @UseGuards()
CInside the service classes
DOnly in the main application bootstrap
✗ Incorrect
Guards are applied on controllers or routes with the @UseGuards() decorator.
What is the main benefit of using role-based guards?
ATo restrict access based on user roles
BTo speed up database queries
CTo style the user interface
DTo log user activity
✗ Incorrect
Role-based guards restrict access to routes depending on user roles.
Explain how role-based guards work in NestJS and how you implement them.
Think about how NestJS checks if a user can access a route based on their role.
You got /5 concepts.
Describe why role-based guards are important for application security.
Consider what happens if anyone could access all parts of an app.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a role-based guard in NestJS?
easy
A. To manage application configuration
B. To handle database connections
C. To format HTTP responses
D. To restrict access to routes based on user roles
Solution
Step 1: Understand role-based guard function
Role-based guards check user roles before allowing access to routes.
Step 2: Identify the correct purpose
Restricting access based on roles matches the guard's role in security.
Final Answer:
To restrict access to routes based on user roles -> Option D
Quick Check:
Role-based guard = restrict access by roles [OK]
Hint: Guards control access; roles limit who can enter [OK]
Common Mistakes:
Confusing guards with database or config management
Thinking guards format responses
Assuming guards handle unrelated tasks
2. Which of the following is the correct way to apply a role-based guard to a route in NestJS?
easy
A. @UseGuards(RolesGuard)\n@Get('profile')
B. @Controller('profile')\n@RolesGuard()
C. @Roles('admin')\n@UseGuards(AuthGuard)
D. @UseGuards(AuthGuard)\n@Get('profile')
Solution
Step 1: Identify correct guard usage syntax
Use @UseGuards with the guard class to apply it to routes.
Step 2: Match with role-based guard usage
@UseGuards(RolesGuard) is correct; @Roles decorator marks roles separately.
Final Answer:
@UseGuards(RolesGuard)\n@Get('profile') -> Option A
Quick Check:
@UseGuards(RolesGuard) applies guard [OK]
Hint: Use @UseGuards with guard class to protect routes [OK]
Common Mistakes:
Using @RolesGuard() instead of @UseGuards
Mixing @Roles and @UseGuards incorrectly
Applying guards without @UseGuards decorator
3. Given this guard code snippet, what will be the result if the user role is 'user' and the route requires 'admin' role?
A. Returns false if no roles metadata, blocking all access
B. Missing async keyword causes runtime error
C. Incorrect use of context.getHandler()
D. Should check user permissions instead of roles
Solution
Step 1: Analyze roles metadata check
If roles metadata is missing, the guard returns false blocking access.
Step 2: Understand typical behavior for missing roles
Usually, missing roles means no restriction, so returning false is too strict.
Final Answer:
Returns false if no roles metadata, blocking all access -> Option A
Quick Check:
No roles metadata should allow access, not block [OK]
Hint: Missing roles metadata should not block access by default [OK]
Common Mistakes:
Assuming missing roles means deny access
Forgetting to handle undefined roles gracefully
Confusing async necessity in this context
5. You want to create a role-based guard that allows access if the user has any one of multiple roles specified on a route. Which approach correctly implements this behavior?
hard
A. Use multiple guards each checking one role and combine them with @UseGuards
B. Check only the first role in roles array against user.role in canActivate
C. Use @Roles('admin', 'editor') decorator and check if user.role is included in roles array inside canActivate
D. Ignore roles and allow access to all authenticated users
Solution
Step 1: Understand multiple roles in @Roles decorator
@Roles can accept multiple roles as arguments to specify allowed roles.
Step 2: Implement canActivate to check inclusion
Checking if user.role is in roles array allows access if user has any allowed role.
Final Answer:
Use @Roles('admin', 'editor') decorator and check if user.role is included in roles array inside canActivate -> Option C
Quick Check:
Check user role in roles array for multiple allowed roles [OK]
Hint: Check if user role is in roles array for multiple roles [OK]
Common Mistakes:
Checking only first role ignores others
Using multiple guards unnecessarily
Allowing all authenticated users ignores roles
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.