-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Welcome to the official wiki for the Production-Grade Authentication Template!
This project provides a robust, secure, and scalable foundation for authentication services built with Node.js, Express, MongoDB, and Redis.
This template is designed for teams and individuals who want a ready-to-use backend authentication solution with best practices for security, scalability, and developer experience.
Core Technologies:
- Node.js (22.14.0+)
- Express
- MongoDB (via Mongoose)
- Redis (via ioredis)
- Docker (for containerization)
- TypeScript types
- Joi (validation)
- Winston (logging)
- Swagger (API docs)
-
Complete Authentication System
User registration, email verification, JWT-based login, password reset, refresh token rotation, session management with Redis. -
Security First Approach
CORS, Helmet, rate limiting, MongoDB sanitization, XSS protection, secure HTTP-only cookies, Joi validation, content security policies. -
Production Ready
Dockerized deployment (dev/prod), Webpack bundling, environment configs, structured logging, health checks, database backup to S3, RabbitMQ integration. -
Developer Experience
Hot reloading, linting/formatting (ESLint, Prettier), git hooks (Husky), comprehensive tests, conventional commit messages, clear project structure.
ποΈ Folder Organization
backend/
βββ docker/ # Docker configuration files
β βββ dev/ # Development Docker setup
β βββ prod/ # Production Docker setup
βββ docs/ # API documentation
β βββ swagger-output.json
β βββ swagger.js
βββ logs/ # Application logs
βββ nginx/ # Nginx configuration for deployment
β βββ http.conf
β βββ https.conf
βββ scripts/ # Utility scripts
β βββ cron.sh
β βββ dbBackup.js
β βββ docker.sh
βββ src/ # Source code
β βββ config/ # Configuration files
β βββ constant/ # Constants and enums
β βββ controllers/ # Request handlers
β βββ db/ # Database connection modules
β βββ helpers/ # Helper utilities
β βββ middlewares/ # Express middlewares
β βββ models/ # Mongoose models
β βββ repository/ # Data access layer
β βββ routes/ # API routes
β βββ services/ # Business logic layer
β βββ types/ # TypeScript type definitions
β βββ utils/ # Utility functions
β βββ validations/ # Input validation schemas
β βββ app.js # Express application setup
β βββ index.js # Application entry point
βββ test/ # Test files
βββ mockData/ # Mock data for tests
βββ routes/ # API route tests
βββ utils/ # Test utilities
βββ validations/ # Validation tests
- JWT authentication with refresh token rotation
- Password hashing (bcrypt)
- Rate limiting
- Robust input validation (Joi)
- HTTP security headers (Helmet)
- Secure cookies
- Protection against NoSQL injection and XSS
Prerequisites:
- Node.js β₯ 22.14.0
- npm β₯ 10.7.0
- MongoDB (latest)
- Redis (latest)
- Docker (optional for deployment)
Installation:
git clone https://github.com/Harmeet10000/ts-backend-production-template.git
cd ts-backend-production-template
npm install
Create a `.env.dev` file in the root directory with the following variables:
```env
# Server
NODE_ENV=development
PORT=3000
SERVER_URL=http://localhost:3000
# Database
DATABASE_URL=mongodb://localhost:27017/auth-service
REDIS_URL=redis://localhost:6379
# JWT
ACCESS_TOKEN_SECRET=your_access_token_secret
REFRESH_TOKEN_SECRET=your_refresh_token_secret
ACCESS_TOKEN_EXPIRY=900
REFRESH_TOKEN_EXPIRY=604800
# Email
EMAIL_HOST=smtp.example.com
EMAIL_PORT=587
EMAIL_USER=your_email@example.com
EMAIL_PASSWORD=your_email_password
EMAIL_FROM=noreply@yourservice.com
# Frontend
FRONTEND_URL=http://localhost:5173
# Backup Configuration
S3_BACKUP_ENABLED=true
S3_BUCKET_NAME=your-backup-bucket
AWS_REGION=us-east-1
S3_PREFIX=mongodb-backups/π NPM Commands
| Command | Description |
|---|---|
npm run dev |
Start the development server with hot reload |
npm run build |
Build the production bundle |
npm run dev:prod |
Run production build with nodemon |
npm start |
Start the production server |
npm run swagger |
Generate Swagger documentation |
npm test |
Run the test suite |
npm run test:watch |
Run tests in watch mode |
npm run test:coverage |
Run tests with coverage report |
npm run lint |
Check code for linting errors |
npm run lint:fix |
Fix linting errors automatically |
npm run format |
Check code formatting |
npm run format:fix |
Fix formatting issues automatically |
npm run migrate:dev |
Run database migrations in development |
npm run migrate:prod |
Run database migrations in production |
π§ Test Commands
Run all tests:
npm testRun tests in watch mode:
npm run test:watchGenerate test coverage report:
npm run test:coverageπ Contribution Guidelines
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'feat: add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the ISC License - see the LICENSE file for details.
Created with β€οΈ by Harmeet Singh