Stateful Auth with MongoDB (Mongoose)
Session-based authentication using MongoDB and Mongoose with server-managed sessions.
Installation Guide
You can add the stateful authentication blueprint to your project using the Servercn CLI:
npx servercn-cli@latest 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 | 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 managementoauth.controller.ts- GitHub & Google OAuth callback handlershealth.controller.ts- Basic and detailed health checks
Services
auth.service.ts- Core authentication business logicemail.service.ts- Email delivery using Nodemailerotp.service.ts- OTP generation, hashing, and verificationupload.service.ts- Cloudinary file upload integration
Models
user.model.ts- User schema with hooks and instance methodssession.model.ts- Session tracking schemaotp.model.ts- One-time password schema
Routes
auth.routes.ts- All authentication endpointsoauth.routes.ts- OAuth flow routeshealth.routes.ts- Health check endpoints