Stateful Authentication with MongoDB (Mongoose)
MongoDB implementation of stateful authentication using Mongoose ODM for session management.
Installation Guide
You can add the stateful authentication blueprint to your project using the Servercn CLI:
npx servercn-cli add blueprint stateful-auth
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 | /google | 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 |
Architecture Comparison
MVC Structure
Feature Structure
Key Files
Controllers
auth.controller.ts - Handles signup, signin, profile management, password operations, session management
oauth.controller.ts - GitHub & Google OAuth callback handlers
health.controller.ts - Basic and detailed health checks
Services
auth.service.ts - Core authentication business logic
email.service.ts - Email delivery using Nodemailer
otp.service.ts - OTP generation, hashing, and verification
upload.service.ts - Cloudinary file upload integration
Models
user.model.ts - User schema with hooks and instance methods
session.model.ts - Session tracking schema
otp.model.ts - One-time password schema
Routes
auth.routes.ts - All authentication endpoints
oauth.routes.ts - OAuth flow routes
health.routes.ts - Health check endpoints
File & Folder Structure
Loading files...
Installation
npx servercn-cli add bp stateful-auth