A high-performance e-commerce backend architecture built with NestJS, TypeScript, and TypeORM. This system implements a robust Role-Based Access Control (RBAC) model for buyers and sellers, utilizing PostgreSQL for persistence and JWT for stateless authentication.
- NestJS: Modular architecture for scalable enterprise applications.
- TypeORM: Advanced Data Mapper pattern for PostgreSQL database management.
- Passport & JWT: Secure authentication flow with customized strategy implementation.
- RBAC: Specialized decorators and guards for
buyer,seller, andadminroles. - Argon2: Industry-standard password hashing and security.
- Docker Integration: Pre-configured containerization for the database environment.
| Technology | Purpose |
|---|---|
| TypeScript | Type-safe development |
| NestJS | Framework architecture |
| PostgreSQL | Relational database |
| TypeORM | Object-Relational Mapping |
| Docker | Environment orchestration |
| Argon2 | Cryptographic hashing |
Ensure you have the following installed:
Clone the repository:
git clone https://github.com/samueltuoyo15/Marketplace-API
cd marketplace-place-apiInstall dependencies:
npm install
# or
pnpm installCreate a .env file in the root directory:
PORT=8080
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=password
DATABASE_NAME=marketplace
JWT_SECRET_KEY=your_complex_secret_key_here
JWT_EXPIRES_IN=3600sStart the PostgreSQL container:
docker-compose up -d# Development mode
npm run start:devThe API uses Stateless JWT Authentication. Upon successful login or registration, the server issues a signed JWT. This token must be included in the Authorization header as a Bearer token for protected routes.
Role enforcement is handled via a combination of:
- Roles Decorator: A custom decorator (
@Roles('admin', 'seller')) used to attach required roles to specific routes. - Roles Guard: A global or method-level guard that:
- Extracts the JWT from the request.
- Decodes the user's role.
- Compares it against the metadata set by the
@Rolesdecorator. - Throws a
403 Forbiddenexception if the role is insufficient.
{
"user_id": 10,
"role": "buyer",
"iat": 1734523053,
"exp": 1734526653
}http://localhost:8080
curl -X POST http://localhost:8080/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "SecurePassword123",
"role": "buyer"
}'curl -X POST http://localhost:8080/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "SecurePassword123"
}'Requires seller role.
curl -X POST http://localhost:8080/products \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "Professional Camera",
"price": 1200.50
}'Public access.
curl -X GET http://localhost:8080/productsRequires buyer role.
curl -X POST http://localhost:8080/orders \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"productId": "1",
"quantity": 2
}'Requires authentication.
curl -X GET http://localhost:8080/orders/me \
-H "Authorization: Bearer YOUR_JWT_TOKEN"Samuel Tuoyo
- GitHub: @samueltuoyo15
- Twitter: @TuoyoSamuel