A comprehensive, production-ready authentication system built with NestJS featuring multi-provider authentication, advanced security measures, and enterprise-level user management.
- Multi-Provider Authentication: Local, GitHub OAuth, Google OAuth
- Advanced Security: Risk assessment, device fingerprinting, geoIP tracking
- Two-Factor Authentication: TOTP with QR code generation
- Session Management: Encrypted JWT sessions with remember me functionality
- Email Integration: Verification emails and password reset
- Rate Limiting: Built-in request throttling
- API Documentation: Swagger/OpenAPI integration
- Geographic Security: Location-based threat detection
- Risk Assessment System: Real-time threat level calculation
- Device Fingerprinting: Tracks user agents and device characteristics
- GeoIP Analysis: Compares login location with known locations
- Threat Scoring: Prevents login if risk level exceeds threshold
- Encrypted Sessions: Custom JWT encryption
- Password Security: bcrypt hashing
- Node.js (v18 or higher)
- PostgreSQL database
- pnpm package manager
- Environment variables configured
This project uses a custom version of geoip-lite.
After running pnpm install, you must manually copy the contents of custom_libs/geoip-lite into node_modules/geoip-lite.
If you skip this step, the app may not work as expected.
Steps:
- Run
pnpm install - Copy the custom library:
cp -r custom_libs/geoip-lite/* node_modules/geoip-lite/ - Continue with your normal workflow.
Important: Repeat this step every time you reinstall dependencies.
# Clone the repository
git clone [<repository-url>](https://github.com/Hckerson/Auth.git)
cd project
# Install dependencies
pnpm install
# Apply custom library patch
cp -r custom_libs/geoip-lite/* node_modules/geoip-lite/Create a .env file in the root directory:
# Database
DATABASE_URL="postgresql://username:password@localhost:5432/auth_db"
# Authentication
COOKIE_SECRET="your-super-secret-cookie-key"
JWT_SECRET="your-jwt-secret-key"
# OAuth Providers
GITHUB_CLIENT_ID="your-github-client-id"
GITHUB_CLIENT_SECRET="your-github-client-secret"
GITHUB_CALLBACK_URL="http://localhost:3000/auth/github/callback"
GOOGLE_CLIENT_ID="your-google-client-id"
GOOGLE_CLIENT_SECRET="your-google-client-secret"
GOOGLE_CALLBACK_URL="http://localhost:3000/auth/google/callback"
AUTHORIZATION_URL="https://accounts.google.com/o/oauth2/v2/auth"
TOKEN_URL="https://oauth2.googleapis.com/token"
# Email Configuration
APP_USERNAME="your-email@gmail.com"
APP_PASSWORD="your-app-password"
SENDER_EMAIL="noreply@yourapp.com"
# Server
PORT=3000# Generate Prisma client
pnpm run prisma:generate
# Run database migrations
npx prisma migrate dev
# (Optional) Seed database
npx prisma db seed# Development mode
pnpm run start:dev
# Production mode
pnpm run start:prod
# Debug mode
pnpm run start:debugOnce the application is running, visit:
- Health Check:
http://localhost:3000/
POST /auth/login- User login with credentialsPOST /auth/signup- User registrationPOST /auth/logout- User logoutPOST /auth/reset-password- Password reset
GET /auth/github- GitHub OAuth loginGET /auth/github/callback- GitHub OAuth callbackGET /auth/google- Google OAuth loginGET /auth/google/callback- Google OAuth callback
POST /auth/2fa/setup- Setup 2FA for userPOST /auth/2fa/verify- Verify 2FA token
POST /auth/send-verification-link- Send verification emailPOST /auth/verify-email- Verify email addressPOST /auth/send-reset-password-link- Send password reset email
# Unit tests
pnpm run test
# E2E tests
pnpm run test:e2e
# Test coverage
pnpm run test:cov
# Debug tests
pnpm run test:debugsrc/
βββ auth/ # Authentication module
β βββ auth.module.ts # Main auth module
β βββ auth.service.ts # Core authentication logic
β βββ auth.controller.ts # API endpoints
β βββ dto/ # Data transfer objects
β βββ entities/ # Database entities
β βββ service/
β βββ passport/ # Passport strategies & guards
β βββ mailtrap.service.ts
βββ lib/ # Utility services
β βββ risk-assesment.service.ts
β βββ speakesy.service.ts
β βββ qr-code.service.ts
β βββ verificationLink.service.ts
βββ prisma/ # Database service
The system calculates a threat level (0-100) based on:
- GeoIP Analysis: Compares login location with user's known locations
- Device Fingerprinting: Tracks user agents and device characteristics
- IP Address Tracking: Monitors login patterns
- Location Changes: Penalizes logins from new locations
- Login Attempt β Risk Assessment β Threat Level Calculation
- If Safe β Validate Credentials β Create Session
- If 2FA Enabled β Require TOTP Token
- Session Management β Encrypted JWT Storage
model User {
id String @id @default(uuid())
email String @unique
password String
username String @default("")
provider String @default("")
emailVerified Boolean @default(false)
twofaVerified Boolean @default(false)
speakeasySecret String?
lastLoginIp String?
lastKnownDevice String?
sessions Session[]
geoData GeoData?
}# Install Mau CLI
pnpm install -g @nestjs/mau
# Deploy to AWS
mau deploy-
Build the application:
pnpm run build
-
Set up environment variables for production
-
Run database migrations:
npx prisma migrate deploy
-
Start the application:
pnpm run start:prod
- 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
- Documentation: NestJS Documentation
- Community: Discord Channel
- Courses: NestJS Courses
- Enterprise Support: Enterprise Support
- NestJS Documentation
- NestJS Devtools
- NestJS Mau - AWS deployment platform
- NestJS Jobs
- Author: Kamil MyΕliwiec
- Website: https://nestjs.com
- Twitter: @nestframework
Built with β€οΈ using NestJS