Overview
Kioto Teteria Backend implements role-based access control (RBAC) to restrict access to specific endpoints based on user roles. Authorization is enforced using theRolesGuard in combination with the @Roles() decorator.
How It Works
Authorization happens after authentication. The system checks if the authenticated user has the required role(s) to access a specific endpoint.Components
- RolesGuard - Guard that checks user roles
- @Roles() Decorator - Decorator to specify required roles
- JWT Payload - Contains user role information
Roles Guard
TheRolesGuard implements the CanActivate interface to perform role checks:
src/guards/roles.guard.ts:6
How the Guard Works
- Uses NestJS’s
Reflectorto read metadata from the route handler - If no roles are specified, access is granted (returns
true) - Extracts the user object from the request (populated by JWT authentication)
- Checks if the user’s role matches any of the required roles
Roles Decorator
The@Roles() decorator attaches role metadata to route handlers:
src/modules/auth/decorators/roles.decorator.ts:3
Usage Example
To protect an endpoint with role-based authorization:src/modules/categories/categories.controller.ts:22
Multiple Guards
Authorization requires authentication. Always use
JwtAuthGuard before RolesGuard in the guards array.JwtAuthGuardvalidates the JWT and attaches user info to the requestRolesGuardchecks if the user has the required role
Current Roles
The system currently defines the following role:ADMIN
The ADMIN role is assigned to admin users in the database and grants access to protected endpoints. From the Admin model (prisma/schema.prisma:14):
Protected Endpoints
The following operations require ADMIN role:Categories
POST /categories- Create categoryPATCH /categories/:id- Update categoryDELETE /categories/:id- Delete category
Products
POST /products- Create productPATCH /products/:id/status- Update product statusDELETE /products/:id- Delete product
src/modules/products/products.controller.ts:39:
Public Endpoints
Endpoints without guards are publicly accessible:GET /categories- List all categoriesGET /products- List products (paginated)GET /products/:id- Get product detailsPOST /auth/login- Admin login
Error Responses
When authorization fails, NestJS returns:Multiple Roles
The@Roles() decorator accepts multiple roles:
Best Practices
Always combine with authentication
Always combine with authentication
The
RolesGuard relies on the user object in the request. Always use JwtAuthGuard before RolesGuard.Apply at method level
Apply at method level
Apply guards to individual route handlers rather than the entire controller for fine-grained control.
Check isActive flag
Check isActive flag
The authentication system already checks the
isActive flag during login. Inactive admins cannot obtain valid tokens.Extending the Role System
To add new roles:- Update the Admin model in
prisma/schema.prismaif you want to use an enum - Assign roles when creating admin users
- Use the
@Roles()decorator with your new role names
Related
- Authentication - Learn about JWT authentication
- Database - Understand the Admin model