Stateless Auth with MySQL (Drizzle)
This blueprint provides a complete stateless authentication system for Express applications using MySQL and Drizzle ORM. It features JWT-based access and refresh tokens, token rotation, and secure cookie management.
Installation
npx servercn-cli add blueprint stateless-authDuring installation, select MySQL (Drizzle) as your database.
Features
This blueprint comes packed with production-ready features organized into a clean, modular structure:
- JWT Stateless Auth: Secure authentication using Access and Refresh tokens with automatic rotation.
- Token Reuse Detection: Advanced security layer that detects and revokes compromised tokens.
- OAuth 2.0 Support: Fully configured Google and GitHub social authentication.
- OTP System: Integrated email-based One-Time Password service for sign-in and resets.
- Profile Management: Built-in support for profile updates and Cloudinary-powered avatar uploads.
- Security Suite: Comprehensive protection with Helmet headers, CORS, and multi-level rate limiting.
- Validated Architecture: Model-View-Controller (MVC) or Feature-based patterns with full Zod validation.
- Error Management: Standardized API response wrappers and centralized global error handling.
- Health Monitoring: Dedicated endpoints for heartbeat and system status checks.
API Endpoints
All endpoints are the same for both MVC and Feature architectures.
Authentication Routes
Base URL: http://localhost:3000/api/v1/auth
| Method | Endpoint | Description |
|---|---|---|
| POST | /verify-otp | Verify OTP for email verification |
| POST | /signup | Register new user |
| POST | /signin | Login user |
| GET | /profile | Get user profile |
| PATCH | /profile | Update profile (with avatar) |
| POST | /logout | Logout current user |
| POST | /forgot-password | Request password reset OTP |
| POST | /reset-password | Reset password with OTP |
| POST | /change-password | Change current password |
| DELETE | /delete-account | Soft delete account |
| PUT | /reactivate-account | Restore deleted account |
OAuth Routes
Base URL: http://localhost:3000/api/auth
| Method | Endpoint | Description |
|---|---|---|
| GET | /github | Initiate GitHub OAuth |
| GET | /github/callback | GitHub OAuth callback |
| GET | Initiate Google OAuth | |
| GET | /google/callback | Google OAuth callback |
Health Check
Base URL: http://localhost:3000/api/v1/health
| Method | Endpoint | Description |
|---|---|---|
| GET | / | Basic health check |
| GET | /detailed | Detailed health status |
Project Structure
The blueprint implements a highly organized directory structure designed for scalability and maintainability:
MVC Structure
Feature Structure
Security Features
- Relational Integrity: Uses foreign keys to link tokens to users.
- Token Rotation: Every refresh cycle invalidates the previous token.
- Type Safety: Full TypeScript support with Drizzle ORM.
- Secure Cookies: HTTP-only and Secure flags for production environments.
Contributing
Found a bug or want to suggest improvements? Please contribute to the Servercn project!
Support
Need help? Join our Discord community or open an issue on GitHub.