Stateless Auth (MySQL)
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.
Project Structure
The blueprint implements a highly organized directory structure designed for scalability and maintainability:
Architecture Support
This blueprint supports both MVC and Feature-based architectures.
MVC Structure
Core Implementation (Drizzle ORM)
1. Authentication Middleware
The verify-auth.ts middleware handles token verification and automatic rotation using refresh tokens stored in MySQL.
2. Drizzle Schemas
Defining the relational structure for users and refresh tokens.
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.