A microservice for handling user authentication and authorization in the NestJS microservices architecture.
- User Authentication: JWT-based authentication with access and refresh tokens
- User Management: CRUD operations for user accounts
- Role-based Authorization: Support for ADMIN and USER roles
- gRPC Microservice: Inter-service communication via gRPC
- REST API: HTTP endpoints for authentication operations
- Database Integration: PostgreSQL with Prisma ORM
- Caching: Redis-based caching for performance
- Internationalization: Multi-language support with nestjs-i18n
- API Documentation: Swagger/OpenAPI documentation
- Health Checks: Built-in health monitoring
- Security: Helmet security headers, CORS configuration
- Framework: NestJS 10.x
- Language: TypeScript 5.x
- Database: PostgreSQL with Prisma ORM
- Cache: Redis with cache-manager
- Authentication: JWT with Passport.js
- API Documentation: Swagger/OpenAPI
- Microservice: gRPC communication
- Validation: class-validator and class-transformer
- Testing: Jest
src/
├── app/ # Application bootstrap
├── common/ # Shared modules and utilities
│ ├── config/ # Configuration management
│ ├── constants/ # Application constants
│ ├── decorators/ # Custom decorators
│ ├── dtos/ # Data Transfer Objects
│ ├── filters/ # Exception filters
│ ├── guards/ # Authentication guards
│ ├── interceptors/ # Response interceptors
│ ├── interfaces/ # TypeScript interfaces
│ ├── middlewares/ # Request middlewares
│ ├── providers/ # JWT strategies
│ └── services/ # Shared services
├── generated/ # gRPC generated code
├── languages/ # i18n translation files
├── modules/ # Feature modules
│ ├── auth/ # Authentication module
│ └── user/ # User management module
└── protos/ # gRPC protocol buffers
- Node.js >= 18.0.0
- npm >= 9.0.0
- PostgreSQL
- Redis
-
Clone the repository
git clone <repository-url> cd auth
-
Install dependencies
npm install
-
Environment Configuration The service includes a pre-configured
.env.docker
file with the following variables:# App Configuration NODE_ENV="local" APP_NAME="@backendworks/auth" APP_CORS_ORIGINS="*" APP_DEBUG=true # HTTP Configuration HTTP_ENABLE=true HTTP_HOST="0.0.0.0" HTTP_PORT=9001 HTTP_VERSIONING_ENABLE=true HTTP_VERSION=1 # Database Configuration DATABASE_URL="postgresql://admin:master123@localhost:5432/postgres?schema=public" # JWT Configuration ACCESS_TOKEN_SECRET_KEY="EAJYjNJUnRGJ6uq1YfGw4NG1pd1z102J" ACCESS_TOKEN_EXPIRED="1d" REFRESH_TOKEN_SECRET_KEY="LcnlpiuHIJ6eS51u1mcOdk0P49r2Crwu" REFRESH_TOKEN_EXPIRED="7d" # Redis Configuration REDIS_URL="redis://localhost:6379" REDIS_KEY_PREFIX="auth:" REDIS_TTL=3600 # gRPC Configuration GRPC_URL="0.0.0.0:50051" GRPC_PACKAGE="auth"
-
Database Setup
# Generate Prisma client npm run prisma:generate # Run migrations npm run prisma:migrate # (Optional) Open Prisma Studio npm run prisma:studio
-
Generate gRPC code
npm run proto:generate
npm run dev
npm run build
npm start
docker build -t auth-service .
docker run -p 9001:9001 auth-service
POST /auth/login
- User loginPOST /auth/signup
- User registrationGET /auth/refresh
- Refresh access token
GET /user/profile
- Get user profilePUT /user/profile
- Update user profile
GET /admin/user
- List all users (paginated)DELETE /admin/user/:id
- Delete user
GET /health
- Service health statusGET /
- Service information
ValidateToken
- Validate JWT tokens and return user information
The service uses a modular configuration system with environment-specific settings:
- Name: Service name and display information
- Environment: Development, staging, production
- Debug: Debug mode settings
- CORS: Cross-origin resource sharing settings
- Port: HTTP server port (default: 9001)
- Host: HTTP server host
- Versioning: API versioning settings
- Access Token: Secret key and expiration time
- Refresh Token: Secret key and expiration time
- URL: PostgreSQL connection string
- Migrations: Database migration settings
- URL: Redis connection string
- Key Prefix: Cache key prefix
- TTL: Cache time-to-live
- URL: gRPC server address
- Package: Protocol buffer package name
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:cov
When running in development mode, Swagger documentation is available at:
http://localhost:9001/docs
- JWT Authentication: Secure token-based authentication
- Password Hashing: bcrypt password hashing
- Role-based Access Control: ADMIN and USER roles
- Helmet Security: Security headers
- CORS Protection: Cross-origin request protection
- Input Validation: Request validation with class-validator
- Rate Limiting: Built-in rate limiting (configurable)
- Health Checks: Built-in health monitoring endpoints
- Sentry Integration: Error tracking and monitoring
- Logging: Structured logging with Winston
- Metrics: Performance metrics collection
Ensure all required environment variables are set in your deployment environment.
Run database migrations before starting the service:
npm run prisma:migrate:prod
The service provides health check endpoints for load balancers and monitoring systems.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
This project is licensed under the MIT License.
For support and questions, please contact the development team or create an issue in the repository.