This Project is a Nest.js application designed for handling product data and providing role-based access control (RBAC). It allows users to create, update, view, and delete products with different levels of permissions based on their roles: Admin, Manager, and Client. Secured with Authentication using JWT, Refresh token rotation.
The project uses the following technologies:
Before starting the project, ensure you have the following installed:
-
Open a terminal and navigate to the project directory.
-
Run the following command to build and start the MongoDB container:
docker-compose up -d
This command creates and runs a MongoDB container using the
docker-compose.yml
file.
- First of all we need to install the required modules in order to spin up our Nest.js Server
npm install
- Then since we are using Prisma we need to generate prisma client that interacts with the databas
npm run db:client:generate
- After that we have to execute the prisma schema and apply it on the database
npm run db:push
- since we will be interacting with products, categories, users, I decided to add a seeder to get you started with testing directly
npm run db:seed
- Now we're ready to startup our server
npm run start:dev
- The server will start up on port
3000
, the base Api path islocalhost:3000/api
NB: To skip all the 5 little steps
of the Step 2
you can just run directly
npm run setup
It will do all of the above at once
Access to API resources is controlled by user roles:
- Admin: Full access to all operations (CRUD) on users, products, and categories.
- Manager: Can create and update products and categories but cannot delete them.
- Client: Can only read/view products and categories.
The application supports three roles:
- Admin: Can manage users, products, and categories.
- Manager: Can create and update products and categories but cannot delete them.
- Client: Can only view products and categories.
Each product includes the following properties:
name
: Name of the product.description
: Detailed information about the product.price
: Product price.category
: Category the product belongs to.stock quantity
: Number of products available in stock.
Categories help organize products. Each category includes:
name
: Name of the category.description
: Description of the category.
- Implement filtering options for products by category and price range.
Admins can manage users through the following actions:
- Create Users: Register users with roles.
- Manage Users: Update user details, assign roles, and delete users.
- Users can register and log in using their email and password.
- Upon login, a JWT access token and a refresh token are returned.
- The access token is short-lived, while the refresh token is long-lived.
- The API allows users to refresh access tokens without re-logging in.
- Admins and Managers can create, read, update, and delete products and categories.
- Clients can only view products and categories.
- JWT Authentication: All protected endpoints require valid JWT access tokens.
- Refresh Token Mechanism: Refresh tokens are used to renew access tokens without requiring a re-login.
- Input Validation: Data like product name, price, stock quantity, and category are validated to ensure integrity.
- Pagination: Pagination for products is implemented to allow fetching results in smaller chunks ☑️
- Soft Deletion: Products and categories can be marked as "deleted" without being removed from the database ☑️
- API Documentation: Use Swagger to document API endpoints☑️ check the url
http://localhost:3000/api/docs
for the documentation
To help you get started quickly, the database has been seeded with the following test users:
Role | Password | |
---|---|---|
Admin | [email protected] | admin123 |
Manager | [email protected] | manager123 |
Client | [email protected] | client123 |
You can use these credentials to log in and test the application. The seeded users have different roles (Admin, Manager, and Client) to demonstrate the role-based access control functionality.