Skip to content

jackynguyen333/rabbitmq-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ NestJS Authentication Server with RabbitMQ Integration

Modern authentication server built with NestJS, JWT, and RabbitMQ message queue.

✨ Features

  • βœ… 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

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Client    │─────▢│ Auth Server  │─────▢│  RabbitMQ   β”‚
β”‚  (Browser)  │◀─────│  (NestJS)    β”‚      β”‚   (Queue)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
                             β–Ό
                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                      β”‚  Consumer   β”‚
                      β”‚  (Listener) β”‚
                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“‹ Prerequisites

  • Node.js v22.20.0+
  • Yarn 4.10.3+
  • RabbitMQ account (CloudAMQP)

πŸš€ Quick Start

1. Install Dependencies

yarn install

2. Configure Environment

Create 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

3. Start Server

# Development mode with hot reload
yarn start:dev

# Production mode
yarn start:prod

Server will start at: http://localhost:4000

4. Access Swagger Documentation

Open your browser: http://localhost:4000/api

πŸ§ͺ Testing RabbitMQ Integration

Quick Test Script

./quick-test.sh

Manual Test with curl

curl -X POST http://localhost:4000/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "test@example.com",
    "username": "testuser",
    "password": "test123456"
  }'

Expected Server Logs

[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": { ... }
}

πŸ“‘ API Endpoints

πŸ”“ Public Endpoints

Method Endpoint Description
POST /auth/register Register new user
POST /auth/login Login user

πŸ”’ Protected Endpoints (Require JWT)

Method Endpoint Description
GET /auth/me Get current user info
POST /auth/logout Logout user

πŸ“¦ RabbitMQ Message Format

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"
}

πŸ§ͺ Run Tests

# Unit tests
yarn test

# Test coverage
yarn test:cov

# E2E tests
yarn test:e2e

πŸ“š Documentation

πŸ”§ RabbitMQ Setup

Create CloudAMQP Account

  1. Go to https://www.cloudamqp.com/
  2. Sign up with GitHub
  3. Create a new instance
  4. Copy your AMQP URL to .env

View Messages in Queue

Access CloudAMQP Management UI:

πŸ› οΈ Tech Stack

  • 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)

πŸ“‚ Project Structure

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

🎯 Use Cases

Messages in RabbitMQ queue can be consumed by:

  1. Email Service - Send verification/welcome emails
  2. Analytics Service - Track user registration metrics
  3. CRM Service - Sync users to CRM system
  4. Notification Service - Send notifications to admins
  5. Data Warehouse - Sync to reporting database
  6. Audit Service - Log user activities

πŸ” Security Features

  • βœ… JWT authentication with configurable expiration
  • βœ… Password hashing with bcrypt
  • βœ… Token blacklisting on logout
  • βœ… Request validation with class-validator
  • βœ… Protected routes with Guards
  • βœ… CORS enabled

πŸ› Troubleshooting

Port already in use

lsof -ti:4000 | xargs kill -9

RabbitMQ connection failed

Check your .env file and ensure RABBITMQ_URL is correct.

Consumer not receiving messages

Verify:

  1. Consumer service is running
  2. Exchange and queue are properly configured
  3. Routing key matches

πŸ“ Scripts

{
  "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"
}

πŸ“„ License

MIT

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“ž Support

For issues and questions, please open an issue on GitHub.


References

  1. https://nestjs.com/
  2. https://www.rabbitmq.com/
  3. https://www.cloudamqp.com/
  4. https://jwt.io/
  5. https://swagger.io/

Made with ❀️ using NestJS and RabbitMQ

About

rabbitmq-backend

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors