Stateful Authentication Blueprint
Stateful authentication is an auth model where the server stores session data for each logged-in user.
A comprehensive stateful authentication system using server-side sessions stored in MongoDB. This blueprint provides secure user registration, login, session management, and OAuth integration.
Installation Guide
You can add the stateful authentication blueprint to your project using the Servercn CLI:
npx servercn-cli add blueprint stateful-authFeatures
- Session-Based Authentication - Server-managed sessions with refresh token support
- User Registration & Login - Secure signup/signin with validation
- Profile Management - Update profile, change password, avatar upload
- Session Management - View active sessions, revoke specific or all sessions
- Password Recovery - Forgot/reset password via OTP
- OAuth Integration - GitHub and Google OAuth support
- Security Features - Rate limiting, account restrictions, soft delete
- Account Reactivation - Restore deleted accounts within grace period
Architecture Options
This blueprint is available in two architectures:
MVC Architecture
Traditional Model-View-Controller pattern with separated concerns:
- Controllers handle business logic
- Models define data schemas
- Services encapsulate complex operations
- Routes define API endpoints
- Middlewares handle cross-cutting concerns
Feature Architecture
Domain-driven modular structure organized by feature:
- Each module contains controller, service, routes, models
- Shared utilities in common directories
- Better scalability for large applications
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 |
Session Management
Base URL: http://localhost:3000/api/v1/auth/sessions
| Method | Endpoint | Description |
|---|---|---|
| GET | /sessions | Get all active user sessions |
| DELETE | /sessions | Revoke all sessions except current |
| DELETE | /sessions/:sessionId | Revoke specific session |
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
MVC Architecture
Feature Architecture
Environment Variables
Create a .env file with the following:
Key Differences from Stateless Auth
- Server-Side Sessions - Sessions stored in database
- Session Revocation - Can invalidate specific/all sessions
- No Refresh Tokens - Uses session-based authentication
- Better Security - Full control over active sessions
- Resource Intensive - Requires database lookups for session validation
When to Use
✅ Choose Stateful Auth when:
- You need to track and manage user sessions
- You want ability to revoke access instantly
- You're building internal/admin applications
- Security is higher priority than scalability
❌ Avoid Stateful Auth when:
- Building microservices (stateless is better)
- You need horizontal scalability
- High-performance APIs with millions of users
- Mobile apps with offline capabilities