Part of the E-commerce Platform microservices architecture.
A microservice responsible for user management and authentication in the e-commerce system. Built with NestJS and PostgreSQL, it provides gRPC endpoints for user operations and authentication services.
- User CRUD operations
- Authentication and authorization
- Role-based access control
- Password hashing and validation
- PostgreSQL integration with TypeORM
- gRPC server implementation
- Service-to-service authentication
- Health checks
- Framework: NestJS
- Language: TypeScript
- Database: PostgreSQL
- ORM: TypeORM
- Communication: gRPC
- Authentication: JWT
- Testing: Jest
- CI/CD: GitHub Actions
{
id: string; // UUID
username: string; // Unique username
email: string; // Unique email
password: string; // Hashed password
roles: UserRole[]; // User roles array
firstName?: string; // Optional first name
lastName?: string; // Optional last name
isActive: boolean; // Account status
createdAt: Date; // Creation timestamp
updatedAt: Date; // Last update timestamp
}
enum UserRole {
USER = 'USER',
ADMIN = 'ADMIN'
}
service UsersService {
rpc ValidateCredentials (ValidateCredentialsRequest) returns (User);
rpc FindOne (UserById) returns (User);
rpc FindByUsername (UserByUsername) returns (User);
rpc Create (CreateUserRequest) returns (User);
rpc Update (UpdateUserRequest) returns (User);
rpc Remove (UserById) returns (Empty);
}
- Clone the repository
- Install dependencies:
npm install
- Set up environment variables:
cp .env.example .env
# Edit .env with your configuration
- Start PostgreSQL:
# Using Docker
docker-compose up -d db
# Or use your local PostgreSQL instance
- Run migrations:
npm run migration:run
- Start the service:
# Development
npm run start:dev
# Production
npm run build
npm run start:prod
# Server
PORT=5002
GRPC_PORT=5052
NODE_ENV=development
# Database
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=postgres
DB_DATABASE=users
# JWT
JWT_SECRET=your-secret-key
JWT_EXPIRATION=1h
# gRPC
GRPC_PORT=5000
Build the image:
docker build -t users-microservice .
Run the container:
docker run -p 3000:3000 -p 5000:5000 users-microservice
# Unit tests
npm run test
# E2E tests
npm run test:e2e
# Test coverage
npm run test:cov
- Password hashing using bcrypt
- Role-based access control
- Service-to-service authentication
- Input validation
- Rate limiting
- Secure configuration management
The service includes health check endpoints to monitor:
- Service status
- Database connection
- gRPC server status
The gRPC service definitions can be found in src/users.proto
. The service implements the following methods:
- Validates user login credentials
- Input: ValidateCredentialsRequest (username, password)
- Returns: User
- Throws: UnauthorizedException for invalid credentials
- Returns a single user by ID
- Input: UserById
- Returns: User
- Throws: NotFoundException if user doesn't exist
- Returns a single user by username
- Input: UserByUsername
- Returns: User
- Throws: NotFoundException if user doesn't exist
- Creates a new user
- Input: CreateUserRequest
- Returns: User
- Throws: ConflictException if username/email exists
- Updates an existing user
- Input: UpdateUserRequest
- Returns: User
- Throws: NotFoundException if user doesn't exist
- Deactivates a user account
- Input: UserById
- Returns: Empty
- Throws: NotFoundException if user doesn't exist
The service implements comprehensive error handling for:
- Authentication errors
- Validation errors
- Database constraints
- Duplicate entries
- Not found resources
- Internal server errors
The service exposes the following monitoring endpoints:
- Health Check:
/health
- Metrics:
/metrics
- OpenAPI Docs:
/docs
- API Gateway - API Gateway and Authentication Service
- Orders Service - Order Management Service
- Products Service - Product Catalog Service
- E-commerce Deployment - Infrastructure and Deployment
For the complete architecture and deployment instructions, visit the E-commerce Platform Deployment Repository.