Foundation OAuth Implementation - Complete Guide
Overview
Architecture
User Flow:
┌─────────────────────────────────────────────────────────────┐
│ │
│ 1. User visits aethex.dev/login │
│ 2. Clicks "Login with Foundation" │
│ 3. Redirected to aethex.foundation /api/oauth/authorize │
│ 4. User authenticates on Foundation │
│ 5. Foundation redirects back to aethex.dev/auth/callback │
│ 6. Backend exchanges code for access token │
│ 7. User profile synced from Foundation to Corp DB │
│ 8. Session established on aethex.dev │
│ 9. Redirected to dashboard │
│ │
└─────────────────────────────────────────────────────────────┘Foundation OAuth Credentials
Foundation OAuth Endpoints
1. Authorization Endpoint
2. Token Exchange Endpoint
3. User Info Endpoint
PKCE Implementation
How PKCE Works
Implementation Details
Frontend Components
1. Login Page (code/client/pages/Login.tsx)
code/client/pages/Login.tsx)2. Foundation OAuth Library (code/client/lib/foundation-oauth.ts)
code/client/lib/foundation-oauth.ts)3. useFoundationAuth Hook (code/client/hooks/use-foundation-auth.ts)
code/client/hooks/use-foundation-auth.ts)Backend Endpoints
1. Callback Handler (code/api/auth/callback.ts)
code/api/auth/callback.ts)2. Token Exchange (POST /auth/callback/exchange)
POST /auth/callback/exchange)Session Management
Session Cookies
Using Access Token
Clearing Session
User Profile Sync
Sync Flow
Upsert Logic
Testing
Local Testing
Error Scenarios
Files Modified/Created
New Files
Modified Files
Deprecated Files
Deployment Checklist
Monitoring
Key Metrics
Logging
Troubleshooting
Problem: "No authorization code received"
Problem: "Invalid state token"
Problem: "Token exchange failed"
Problem: User not synced to database
FAQ
Summary
Last updated
