Modern authentication server built with NestJS, JWT, and RabbitMQ message queue.
- β User Authentication - Register, Login, Logout with JWT
- β RabbitMQ Integration - Auto-push user data to message queue on registration
- β Password Security - bcrypt hashing
- β
Swagger Documentation - Interactive API docs at
/api - β TypeScript - Full type safety
- β Unit Tests - Comprehensive test coverage
- β Validation - class-validator for request validation
βββββββββββββββ ββββββββββββββββ βββββββββββββββ
β Client βββββββΆβ Auth Server βββββββΆβ RabbitMQ β
β (Browser) ββββββββ (NestJS) β β (Queue) β
βββββββββββββββ ββββββββββββββββ βββββββββββββββ
β
βΌ
βββββββββββββββ
β Consumer β
β (Listener) β
βββββββββββββββ
- Node.js v22.20.0+
- Yarn 4.10.3+
- RabbitMQ account (CloudAMQP)
yarn installCreate or update .env file:
NODE_PORT=4000
RABBITMQ_URL=amqps://your-rabbitmq-url
RABBITMQ_EXCHANGE_VALIDATE_JSON=validateJSON
RABBITMQ_TOPIC_EXCHANGE_VALIDATE_JSON=topic_validateJSON
RABBITMQ_ROUTING_KEY_VALIDATE_JSON=validateJSON
RABBITMQ_TOPIC_ROUTING_KEY_VALIDATE_JSON=topic_validateJSON
RABBITMQ_QUEUE_VALIDATE_JSON=topic_validateJSON
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production
JWT_EXPIRATION=24h# Development mode with hot reload
yarn start:dev
# Production mode
yarn start:prodServer will start at: http://localhost:4000
Open your browser: http://localhost:4000/api
./quick-test.shcurl -X POST http://localhost:4000/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "test@example.com",
"username": "testuser",
"password": "test123456"
}'[AuthService] New user registration: test@example.com
[RabbitmqProducerService] Message sent successfully
[AuthService] User registration sent to RabbitMQ: test@example.com
[RabbitmqConsumerService] Received message: {
"event": "user.registered",
"data": { ... }
}
| Method | Endpoint | Description |
|---|---|---|
| POST | /auth/register |
Register new user |
| POST | /auth/login |
Login user |
| Method | Endpoint | Description |
|---|---|---|
| GET | /auth/me |
Get current user info |
| POST | /auth/logout |
Logout user |
When a user registers, this message is sent to RabbitMQ:
{
"event": "user.registered",
"data": {
"id": "user_1729360635123_abc",
"email": "newuser@example.com",
"username": "newuser",
"createdAt": "2025-10-19T16:37:15.123Z"
},
"timestamp": "2025-10-19T16:37:15.456Z"
}# Unit tests
yarn test
# Test coverage
yarn test:cov
# E2E tests
yarn test:e2e- RABBITMQ-USER-REGISTRATION.md - RabbitMQ integration details
- FLOW-DIAGRAM.md - Visual flow diagram
- RABBITMQ-INTEGRATION.md - Complete integration guide
- Go to https://www.cloudamqp.com/
- Sign up with GitHub
- Create a new instance
- Copy your AMQP URL to
.env
Access CloudAMQP Management UI:
- URL: https://chimpanzee.rmq.cloudamqp.com
- Navigate to Queues β
topic_validateJSON
- Framework: NestJS v11.1.6
- Language: TypeScript v5.9.3
- Authentication: JWT with Passport
- Password Hashing: bcrypt v6.0.0
- Message Queue: RabbitMQ (amqplib v0.5.5)
- API Documentation: Swagger/OpenAPI v11.2.1
- Validation: class-validator v0.14.2
- Testing: Jest v30.2.0
- Package Manager: Yarn v4.10.3 (PnP)
src/
βββ auth/ # Authentication module
β βββ auth.controller.ts # REST API endpoints
β βββ auth.service.ts # Business logic + RabbitMQ
β βββ auth.module.ts # Module definition
β βββ dto/ # Data transfer objects
β βββ guards/ # JWT auth guard
β βββ strategies/ # Passport strategies
βββ users/ # Users module
β βββ users.service.ts # User CRUD operations
β βββ entities/ # User entity
βββ rabbitmq/ # RabbitMQ module
β βββ rabbitmq-producer.service.ts
β βββ rabbitmq-consumer.service.ts
β βββ rabbitmq.module.ts
βββ common/ # Shared utilities
β βββ decorators/ # Custom decorators
β βββ filters/ # Exception filters
β βββ interceptors/ # Response interceptors
β βββ exceptions/ # Custom exceptions
βββ config/ # Configuration
β βββ index.ts # Config loader
βββ main.ts # Application entry point
Messages in RabbitMQ queue can be consumed by:
- Email Service - Send verification/welcome emails
- Analytics Service - Track user registration metrics
- CRM Service - Sync users to CRM system
- Notification Service - Send notifications to admins
- Data Warehouse - Sync to reporting database
- Audit Service - Log user activities
- β JWT authentication with configurable expiration
- β Password hashing with bcrypt
- β Token blacklisting on logout
- β Request validation with class-validator
- β Protected routes with Guards
- β CORS enabled
lsof -ti:4000 | xargs kill -9Check your .env file and ensure RABBITMQ_URL is correct.
Verify:
- Consumer service is running
- Exchange and queue are properly configured
- Routing key matches
{
"start:dev": "Start development server with hot reload",
"start:prod": "Start production server",
"test": "Run unit tests",
"test:cov": "Run tests with coverage",
"test:e2e": "Run end-to-end tests"
}MIT
Contributions are welcome! Please feel free to submit a Pull Request.
For issues and questions, please open an issue on GitHub.
- https://nestjs.com/
- https://www.rabbitmq.com/
- https://www.cloudamqp.com/
- https://jwt.io/
- https://swagger.io/
Made with β€οΈ using NestJS and RabbitMQ