NovelNest is an online bookstore platform built with a microservices architecture. This repository contains the User Service component, which handles user authentication, profile management, and purchase history tracking.
- Backend: Node.js, Express.js, TypeScript
- Authentication: SuperTokens
- Database: PostgreSQL with Prisma ORM
- Messaging: RabbitMQ for event processing
- Container: Docker, Docker Compose
- Orchestration: Kubernetes
- JWT: JSON Web Tokens for API authentication
user-service/
├── docker-compose.yaml # Local development environment setup
├── Dockerfile # Container image definition
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
├── .env # Environment variables (not for production)
├── prisma/ # Database schema and migrations
│ ├── schema.prisma # Database model definitions
│ └── migrations/ # Database migration files
├── src/ # Source code
│ ├── config.ts # SuperTokens configuration
│ ├── db.ts # Database operations
│ ├── index.ts # Main application entry point
│ ├── middleware.ts # Express middleware (JWT verification)
│ ├── types.ts # TypeScript type definitions
│ └── utils.ts # Utility functions
└── hack/ # Helper scripts
└── create-supertokens-admin.sh # Setup script for admin users
- User Authentication
- Registration and login using SuperTokens
- JWT-based API authentication
- Session management
- User Profile Management
- User metadata storage and retrieval
- Purchase History
- Track and display user book purchases
- Store order status and details
- Event Processing
- Consumer for RabbitMQ order events
- Real-time purchase updates
- SuperTokens Admin Dashboard
- User management interface
- User roles management
Before you begin, ensure you have the following installed:
- Docker and Docker Compose
- Node.js (v18 or later)
- Yarn package manager
- Kubernetes for production deployment (optional)
- kubectl for Kubernetes management (optional)
Create a .env
file in the root directory with the following variables (already provided in the repository):
SUPERTOKENS_DATABASE_URL="postgresql://user:password@postgres-auth:5432/users_db"
USER_DATABASE_URL="postgresql://user:password@postgres-user:5432/users_db"
SUPERTOKENS_CORE_URL="http://supertokens-service:3567"
RABBITMQ_URL="amqp://rabbitmq"
USERSERVICE_API_URL="http://localhost:5000"
VITE_APP_UI_URL="http://localhost:5173"
SUPERTOKENS_DASHBOARD_ADMIN_EMAIL="[email protected]"
SUPERTOKENS_DASHBOARD_ADMIN_PASSWORD="password123"
QUEUE_NAME=orders
PORT=5000
VITE_APP_PORT=5173
JWT_SECRET=qwertyuiopasdfghjklzxcvbnm123456
The easiest way to run the service locally is using Docker Compose:
# Start all services
docker-compose up -d
# Check logs
docker-compose logs -f user-service
# Run database migrations if needed
docker-compose exec user-service yarn migrate
# Generate Prisma client
docker-compose exec user-service yarn generate
This will start:
- PostgreSQL instances for user data and SuperTokens
- SuperTokens authentication service
- The User Service API
- A script to create an admin user for the SuperTokens dashboard
If you prefer to run the service directly:
# Install dependencies
yarn install
# Generate Prisma client
yarn generate
# Run database migrations
yarn migrate
# Start the development server with hot reloading
yarn dev:hot
Note: You'll need to have PostgreSQL and RabbitMQ running separately.
The service exposes the following REST endpoints:
POST /api/auth/register
- Register a new userPOST /api/auth/login
- Log in an existing userPOST /api/auth/logout
- Log out the current userGET /api/auth/user
- Get the current user's profile (authenticated)
GET /api/user/purchases
- Get the current user's purchase history (authenticated)
The service consumes order events from RabbitMQ:
ORDER_CREATED
- When a user purchases a bookORDER_UPDATED
- When an order status changes
The NovelNest application includes Kubernetes manifests for production deployment located in the k8s-manifests/user-service
directory.
# Navigate to the k8s-manifests directory
cd ../k8s-manifests
# Deploy the user service and dependencies
./deploy-user-service.sh
# To deploy the entire application
./deploy-all.sh
The deployment includes:
- PostgreSQL databases for user data and SuperTokens
- SuperTokens service for authentication
- User service API deployment and service
- Necessary migrations
After deployment, you can access the SuperTokens dashboard for user management:
-
Port-forward the SuperTokens service:
kubectl port-forward svc/supertokens 3567:3567 -n novelnest
-
Access the dashboard at
http://localhost:3567/auth/dashboard
-
Log in with the admin credentials defined in environment variables:
- Email:
[email protected]
- Password:
password123
- Email:
You can test the API endpoints using tools like Postman or curl.
Example login request:
curl -X POST http://localhost:5000/api/auth/login \
-H "Content-Type: application/json" \
-d '{"email": "[email protected]", "password": "yourpassword"}'
# Stop and remove containers, networks, and volumes
docker-compose down -v
# Remove user service resources
kubectl delete -f user-service.yaml
kubectl delete -f postgres-user.yaml
kubectl delete -f postgres-auth.yaml
kubectl delete -f supertokens.yaml
# Or to remove all NovelNest resources
kubectl delete namespace novelnest
We welcome contributions to the NovelNest project! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
This README is for the user-service component of the NovelNest application, a microservices-based online bookstore platform.