Authentication Architecture¶
Atlas ERP uses Better Auth as its core authentication provider, replacing legacy passport-based strategies for a more modern, secure, and flexible solution.
Core Concepts¶
Better Auth provides a comprehensive set of features out-of-the-box: - Stateful Sessions: Stored in PostgreSQL via Prisma with storeSessionInDatabase: true. - Secondary Storage Caching: Integrated with Redis to accelerate session validation and manage OTP cooldowns. - OAuth Providers: Easy integration with Google, Microsoft, GitHub, etc. - Two-Factor Authentication (2FA): Support for TOTP via authenticator apps. - Magic Links & Email OTP: Passwordless login options and email verification workflows.
Authentication Flow¶
sequenceDiagram
participant User
participant Frontend as Next.js Web
participant API as NestJS API
participant Auth as Better Auth Service
participant Redis as Redis Cache
participant DB as PostgreSQL
User->>Frontend: Enter credentials
Frontend->>API: POST /api/v1/auth/sign-in/email
API->>Auth: Pass request to nodeHandler
Auth->>DB: Verify user credentials
DB-->>Auth: User matched
Auth->>DB: Create AuthSession record
Auth->>Redis: Cache session in secondary storage
Auth-->>API: Return session context
API-->>Frontend: Set HttpOnly Cookie (better-auth.session_token)
Frontend-->>User: Redirect to /dashboard
Note over Frontend,API: Subsequent Authenticated Request
Frontend->>API: GET /api/v1/projects (with Cookie)
API->>Auth: AuthGuard calls validateSession()
Auth->>Redis: Check session cache
Redis-->>Auth: Session valid
Auth-->>API: Attach user to Request context
API->>DB: Process request
DB-->>API: Return application data
API-->>Frontend: 200 OK with Data
Integrating with NestJS¶
While Better Auth handles the token generation and validation, NestJS enforces the protection on routes using Guards.
The AuthGuard¶
Routes that require a logged-in user use the standard NestJS AuthGuard.
Security Measures¶
- HttpOnly Cookies: Tokens are typically stored in HttpOnly, Secure cookies to mitigate Cross-Site Scripting (XSS) attacks.
- CSRF Protection: Tokens submitted via headers or body (if not using cookies) are validated against CSRF tokens.
- Rate Limiting: Login, registration, and password reset endpoints are strictly rate-limited using
ThrottlerGuardto prevent brute-force attacks. - CAPTCHA: Registration and sensitive actions are protected by Cloudflare Turnstile (
TurnstileGuard).