A robust authentication backend API built with NestJS, featuring comprehensive user management, role-based access control, and modern authentication flows.
- Multi-provider Authentication: Local email/password and Google OAuth2
- Role-Based Access Control (RBAC): Admin, Customer, and Signup roles with permissions
- JWT Authentication: Secure token-based authentication with refresh tokens
- OTP Verification: Email-based OTP for registration and password reset
- Redis Caching: High-performance caching with Redis for improved scalability
- Database Management: PostgreSQL with Prisma ORM
- API Documentation: Swagger/OpenAPI documentation
- Rate Limiting: Built-in rate limiting for security
- Email Service: Configurable email service integration
- Docker Support: Containerized development environment
- Framework: NestJS - Progressive Node.js framework
- Database: PostgreSQL with Prisma ORM
- Cache: Redis for session storage and caching
- Authentication: Passport.js with JWT strategy
- Validation: class-validator and class-transformer
- Documentation: Swagger integration
- Testing: Jest testing framework
- Code Quality: ESLint and Prettier
- Node.js: 24.7.0 (specified in package.json engines)
- npm: Latest version
- Docker: For running PostgreSQL and Redis
- Git: For version control
git clone <repository-url>
cd backendnpm installCreate a .env file in the root directory:
cp .env.example .envdocker-compose up -dThis will start:
- PostgreSQL database on port
5433 - Redis cache on port
6380 - MailHog for email testing on ports
1025(SMTP) and8025(Web UI)
# Generate Prisma client
npm run prisma:generate
# Run database migrations
npm run prisma:migratenpm run start:devThe API will be available at http://localhost:3000
Visit http://localhost:3000/api to access the Swagger documentation.
src/
βββ cache/ # Redis caching system
βββ common/ # Shared utilities
β βββ decorators/ # Common decorators
β βββ dto/ # Base DTOs
β βββ filters/ # Exception filters
β βββ interceptors/ # Response interceptors
β βββ schemas/ # Base response schemas
β βββ utils/ # Utility functions
βββ config/ # Configuration management
βββ database/ # Database module (Prisma)
βββ modules/ # Business logic modules
β βββ auth/ # Authentication module
β β βββ decorators/ # Custom decorators (@Auth, @Roles, @CurrentUser)
β β βββ dtos/ # Data Transfer Objects
β β βββ guards/ # Authentication guards
β β βββ schemas/ # Response schemas
β β βββ services/ # Password and token services
β β βββ strategies/ # Passport strategies (JWT, Local)
β βββ account/ # Account management
β βββ role/ # Role management
β βββ user/ # User management
βββ services/ # External services
βββ email/ # Email service
βββ otp/ # OTP service
Create a .env file with the following variables:
# Application Configuration
APPLICATION_PORT=3000
ALLOWED_ORIGINS=https://localhost2:3005,http://localhost:3001,https://yourdomain.com
# Database Configuration
POSTGRES_URI=postgresql://postgres:password@localhost:5433/fullstack_auth
POSTGRES_HOST=localhost
POSTGRES_PORT=5433
POSTGRES_USER=postgres
POSTGRES_PASSWORD=password
POSTGRES_DB=fullstack_auth
# Redis Configuration
REDIS_HOST=localhost
REDIS_PORT=6380
REDIS_USER=
REDIS_PASSWORD=redis_password
# JWT Configuration
SESSION_SECRET=your-super-secret-jwt-key-here
SESSION_EXPIRES_IN=15m
SESSION_REFRESH_EXPIRES_IN=7d
RESET_PASSWORD_EXPIRES_IN=1h
# Google OAuth Configuration
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
GOOGLE_CALLBACK_URL=http://localhost:3000/auth/google/callback| Variable | Description | Example |
|---|---|---|
APPLICATION_PORT |
Port for the NestJS application | 3000 |
ALLOWED_ORIGINS |
CORS allowed origins (comma-separated) | https://localhost2:3005,http://localhost:3001 |
POSTGRES_URI |
Complete PostgreSQL connection string | postgresql://user:pass@host:port/db |
POSTGRES_HOST |
PostgreSQL host | localhost |
POSTGRES_PORT |
PostgreSQL port | 5433 |
POSTGRES_USER |
PostgreSQL username | postgres |
POSTGRES_PASSWORD |
PostgreSQL password | password |
POSTGRES_DB |
PostgreSQL database name | fullstack_auth |
REDIS_HOST |
Redis host | localhost |
REDIS_PORT |
Redis port | 6380 |
REDIS_USER |
Redis username (optional) | `` |
REDIS_PASSWORD |
Redis password | redis_password |
SESSION_SECRET |
JWT secret key (use a strong secret) | your-super-secret-jwt-key |
SESSION_EXPIRES_IN |
JWT access token expiration | 15m |
SESSION_REFRESH_EXPIRES_IN |
JWT refresh token expiration | 7d |
RESET_PASSWORD_EXPIRES_IN |
Password reset token expiration | 1h |
GOOGLE_CLIENT_ID |
Google OAuth client ID | your-google-client-id |
GOOGLE_CLIENT_SECRET |
Google OAuth client secret | your-google-client-secret |
GOOGLE_CALLBACK_URL |
Google OAuth callback URL | http://localhost:3000/auth/google/callback |
Note about CORS: The
ALLOWED_ORIGINSsetting only affects web browsers. Mobile applications (React Native, Flutter, etc.) bypass CORS restrictions as they use native HTTP clients. For mobile app security, rely on JWT authentication, API keys, and rate limiting instead.
# Development
npm run start:dev # Start development server with hot reload
npm run start:debug # Start development server with debugging
# Production
npm run build # Build the application
npm run start:prod # Start production server
# Database
npm run prisma:generate # Generate Prisma client
npm run prisma:migrate # Run database migrations
npm run prisma:deploy # Deploy migrations to production
# Code Quality
npm run lint # Run ESLint
npm run format # Format code with Prettier
# Testing
npm run test # Run unit tests
npm run test:watch # Run tests in watch mode
npm run test:cov # Run tests with coverage
npm run test:e2e # Run end-to-end tests- User provides email and password
- System sends OTP to email for verification
- User verifies OTP
- Account is created with
SIGNUProle - User can complete profile setup
- User provides email and password
- System validates credentials
- JWT access and refresh tokens are issued
- User is authenticated
- User clicks "Login with Google"
- Redirected to Google OAuth
- Google returns authorization code
- System exchanges code for user info
- User account is created/updated
- JWT tokens are issued
- User requests password reset
- System sends OTP to email
- User verifies OTP
- User sets new password
- Password is updated
The application uses the following main entities:
- User: Core user information
- Account: Authentication provider accounts (Local, Google)
- Role: User roles (Admin, Customer, Signup)
- Permission: Granular permissions for resources
- Token: JWT refresh tokens
- UserRole: Many-to-many relationship between users and roles
- RolePermission: Many-to-many relationship between roles and permissions
- Build the Docker image:
docker build -t fullstack-auth-backend .- Run with docker-compose:
docker-compose up -d- Use environment-specific
.envfiles - Set up proper SSL certificates
- Configure production database
- Set up monitoring and logging
- Use process managers like PM2
- Configure reverse proxy (nginx)
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
This project is licensed under the UNLICENSED License.
If you encounter any issues or have questions:
- Check the Issues page
- Create a new issue with detailed information
- Contact the development team
Happy Coding! π