A secure and modular API built using NestJS with user authentication, role-based access control, and CRUD functionality for products.
Features Authentication: User registration and login using JWT. Role-Based Access Control: Admin and User roles with restricted access. Product Management: Create, read, update, and delete products. Validation: Robust data validation using class-validator. Environment Configuration: Easy setup using .env files. Setup Instructions
Node.js: Ensure Node.js is installed npm: Comes with Node.js. Ensure npm is installed. MongoDB: Install and run a MongoDB instance locally or use a cloud-hosted MongoDB URI.
git clone <repository-url>
cd <repository-folder>
npm install
Create a .env file in the root of the project with the following variables:
PORT=3000
JWT_EXPIRES_IN=3600s
MONGODB_URI=mongodb://localhost:27017/nest-api
Replace your_jwt_secret with a secure string.
Adjust MONGODB_URI if you're using a cloud-hosted MongoDB instance.
npm run start
The API will be accessible at http://localhost:3000.
| Method | Endpoint | Description |
|---|---|---|
| POST | /auth/register |
Register a new user |
| POST | /auth/login |
Login and get a JWT token |
| Method | Endpoint | Description |
|---|---|---|
| POST | /products |
Create a new product |
| GET | /products |
Get all products |
| GET | /products/:id |
Get product by ID |
| PATCH | /products/:id |
Update product by ID |
| DELETE | /products/:id |
Delete product by ID |
- Admin: Can create, update, delete products and manage user roles.
- User: Can create, update, and view products they own.
- Please import the file called NestJS_API_Collection.json to postman to view the collection
- Use the Register and Login endpoints to get JWT tokens.
- Add the JWT token to the
Authorizationheader for protected routes.