A production-ready authentication API built with Express.js, TypeScript, and Prisma ORM featuring JWT token management, refresh token rotation, and comprehensive security monitoring.
- Secure Authentication: JWT-based authentication with refresh token rotation
- User Management: Registration, login, password changes, and profile management
- Security Monitoring: Login attempt tracking, audit logging, and brute force protection
- Database Integration: PostgreSQL with Prisma ORM
- API Documentation: Interactive Swagger/OpenAPI documentation
- Production Ready: Comprehensive logging, error handling, and health checks
- Node.js 20+
- PostgreSQL database
- pnpm (recommended) or npm
# Clone and install dependencies
pnpm install
# Set up environment variables
cp .env.example .env
# Edit .env with your database URL and JWT secrets
# Generate Prisma client and run migrations
pnpm db:generate
pnpm db:migrate
pnpm db:seed
# Start development server
pnpm devNODE_ENV=development
PORT=3000
DATABASE_URL=postgresql://user:password@localhost:5432/auth_db
JWT_SECRET=your-super-secure-jwt-secret-min-32-chars
JWT_EXPIRES_IN=24h
JWT_REFRESH_SECRET=your-super-secure-refresh-secret-min-32-chars
JWT_REFRESH_EXPIRES_IN=7d
ALLOWED_ORIGINS=http://localhost:3000,http://localhost:5173POST /api/auth/register- Create new accountPOST /api/auth/login- Authenticate userPOST /api/auth/refresh- Refresh access tokenPOST /api/auth/logout- Logout user
GET /api/auth/me- Get current user profilePOST /api/auth/change-password- Change passwordGET /api/auth/devices- List active sessions
GET /health- Health checkGET /api/docs- API documentation
The system uses PostgreSQL with the following main tables:
users- User accounts and profilesrefresh_tokens- JWT refresh tokens with rotationlogin_attempts- Security monitoringaudit_logs- Comprehensive activity trackingpassword_resets- Password reset tokensemail_verifications- Email verification tokens
- Password Security: bcrypt hashing with 12 salt rounds
- Token Management: JWT with automatic refresh token rotation
- Rate Limiting: Configurable limits on authentication endpoints
- Audit Logging: Complete activity tracking for compliance
- Input Validation: Zod schema validation on all inputs
- CORS Protection: Configurable origin restrictions
# Development
pnpm dev # Start with hot reload
pnpm dev:debug # Start with debugger
# Database
pnpm db:generate # Generate Prisma client
pnpm db:push # Push schema changes
pnpm db:migrate # Run migrations
pnpm db:studio # Open Prisma Studio
pnpm db:seed # Seed test data
# Production
pnpm build # Build for production
pnpm start # Start production server
# Quality
pnpm type-check # TypeScript validation
pnpm lint # ESLint checkAfter running pnpm db:seed:
Email: admin@example.com
Email: user@example.com
Password: TestPassword123!Interactive API documentation is available at /api/docs when the server is running.
- Express.js: Web framework with TypeScript
- Prisma ORM: Type-safe database access
- JWT: Stateless authentication with refresh tokens
- Pino: Structured logging
- Zod: Runtime type validation
- Swagger: API documentation
- Set
NODE_ENV=production - Configure secure JWT secrets (32+ characters)
- Set up PostgreSQL database
- Configure CORS origins
- Enable helmet security headers
- Set up proper logging aggregation
MIT