A RESTful authentication and CRUD API built with Node.js, Express.js, and MongoDB.
This project demonstrates secure user authentication, role-based authorization, and product management using protected API endpoints.
Hosted on Render.
##Project Overview
This API provides:
- User registration and login
- Secure authentication using hashed passwords and JWT
- Role-based authorization (user, admin)
- CRUD operations on products
- Protected routes that require a valid token
- Admin-only operations such as deleting users or creating products
Admins have elevated access and can:
- Create, update, and delete products
- View all users
- Delete a user
- Clone the repository
- Install your dependencies
- Create your own .env file
- Start the server
Base URL:
Main route groups:
- /api/auth
- /api/product
- /api/user
-
User registers with name, email, password.
-
Password is hashed using bcrypt before saving.
-
User logs in using email and password.
-
Server returns a signed JWT containing user ID and role.
-
Client must attach the token to protected routes using: Authorization: Bearer
-
Authentication middleware verifies JWT.
-
Role-based middleware ensures only admins access admin routes.
A complete Postman Collection is included for easy testing, containing:
- All authentication routes
- All product CRUD routes
- Admin routes
- Example requests & responses
- Token authentication flow
Download the Postman Collection here: https://documenter.getpostman.com/view/46739172/2sB3WvNJac
- POST /api/auth/register
-
request: The same also for the user, if no role is put, it adds user by default. { "name": "Steph", "email": "steph@example.com", "password": "mypassword", "role": "admin" }
-
response { "status": "success", "message": "User registered successfully", "data": { "user": {}, "token": "..." } }
- POST /api/auth/login
-
request { "email": "steph@example.com", "password": "mypassword" }
-
response { "status": "success", "message": "User logged in successfully ", "data": { "user": {}, "token": "..." } }
- POST /api/product/create (admin only) requires token.
-
request { "name": "Television", "description": "Sleek flat screen television", "price": 125000, "inStock": true }
-
response { "status": "success", "message": "Product created successfully", "data": { } }
- GET /api/product/all - Get all products (public).
-
request No request needed
-
response { "status": "success", "data": [{}, {}, {},....] }
- GET /api/product/:id Add the id of the product you want to get and send
- response { "status": "success", "data": {} }
- PATCH /api/product/:id/update (admin only) - update products and requires token.
-
request (also add the id of the product in your request head) { "name": "Television", "description": "Sleek flat screen television", "price": 150000, "inStock": false }
-
response { "status": "success", "message": "Product updated successfully" }
- DELETE /api/product/:id/delete (admin only) requires token.
-
request Add the id of the product you want to delete and send the request
-
response { "status": "success", "message": "Product deleted Successfully" }
- GET /api/user/users/ (Admin only) — gets all users. (requires admin token)
-
request No request needed
-
response { "status": "success", "data": [{}, {}, {},....] }
- DELETE /api/user/:id (Admin only) - delete a user. (requires admin token)
-
request Add the id of the user you want to delete and send the request
-
response { "status": "success", "message": "User deleted successfully" }
200 – OK
201 – created
400 – Bad request (validation errors)
401 – Unauthorized (missing/invalid token)
403 – Forbidden (not an admin)
404 – Resource not found
500 – Server error
The API uses centralized error handling middleware to ensure consistent JSON error responses:
- Invalid or expired tokens
- Missing required fields
- Validation errors
- Not found errors
- Server errors