📖 Scenario: You are building a simple NestJS backend for a company. Different users have different roles like admin and user. You want to control access to certain routes based on these roles.
🎯 Goal: Create a role-based authorization system in NestJS that allows only users with the admin role to access a protected route.
📋 What You'll Learn
Create a roles array to define user roles
Create a variable to hold the current user's role
Use a guard or function to check if the user role matches the required role
Protect a route so only admin role users can access it
💡 Why This Matters
🌍 Real World
Role-based authorization is used in real apps to control who can see or do what, like only managers can approve requests.
💼 Career
Understanding role-based access control is important for backend developers to secure APIs and protect sensitive data.
Progress0 / 4 steps
1
Define user roles array
Create a constant array called roles with the exact strings 'admin' and 'user'.
NestJS
Hint
Use const roles = ['admin', 'user']; to define the roles array.
2
Set current user role
Create a variable called currentUserRole and set it to the string 'user'.
NestJS
Hint
Use let currentUserRole = 'user'; to set the current user role.
3
Create role check function
Write a function called canAccessAdmin that returns true if currentUserRole is exactly 'admin', otherwise returns false.
NestJS
Hint
Use a simple function that compares currentUserRole to 'admin' and returns the result.
4
Protect route with role check
Create a NestJS controller method called getAdminData that returns the string 'Secret admin data' only if canAccessAdmin() returns true. Otherwise, throw an UnauthorizedException. Import UnauthorizedException from @nestjs/common.
NestJS
Hint
Use an if statement to check canAccessAdmin(). Throw UnauthorizedException if false, else return the secret string.
Practice
(1/5)
1. What is the main purpose of role-based authorization in a NestJS application?
easy
A. To handle HTTP request parsing
B. To manage database connections
C. To control access to routes based on user roles
C. Using getHandler() instead of getClass() for roles
D. Not checking if user exists before accessing user.role
Solution
Step 1: Review user role access
The code accesses user.role without verifying if user is defined.
Step 2: Identify potential runtime error
If user is undefined, accessing user.role causes an error.
Final Answer:
Not checking if user exists before accessing user.role -> Option D
Quick Check:
Always check user before role access [OK]
Hint: Check user object exists before role check [OK]
Common Mistakes:
Assuming user is always present
Confusing getHandler() and getClass() usage
Thinking async is required for synchronous guard
5. How can you combine role-based authorization with route-level guards to allow access only if the user has either 'admin' or 'manager' role in NestJS?
hard
A. Use @Roles('admin', 'manager') decorator and a RolesGuard that checks for any matching role
B. Use two separate @Roles decorators, one for 'admin' and one for 'manager', on the same route
C. Use @UseGuards(AdminGuard, ManagerGuard) without @Roles decorator
D. Check roles manually inside the controller method without decorators or guards
Solution
Step 1: Understand role decorator usage
@Roles() accepts multiple roles to allow any of them access.
Step 2: Use a guard that checks if user role matches any allowed role
The RolesGuard reads roles from metadata and checks if user role is included.
Final Answer:
Use @Roles('admin', 'manager') decorator and a RolesGuard that checks for any matching role -> Option A
Quick Check:
Multiple roles in @Roles() + guard = flexible access [OK]
Hint: Pass multiple roles in one @Roles() decorator [OK]
Common Mistakes:
Using multiple @Roles decorators on one route
Applying multiple guards instead of one flexible guard
Skipping decorators and guards for manual checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.