|
| 1 | +# Authentication Architecture |
| 2 | + |
| 3 | +## 🚨 CRITICAL: Environment-Specific Authentication Rules |
| 4 | + |
| 5 | +**This document defines the EXACT authentication behavior for each environment. Never deviate from these rules.** |
| 6 | + |
| 7 | +## Authentication by Environment |
| 8 | + |
| 9 | +### 🏠 **Local Development** |
| 10 | +- **Auth System**: Dev Auth (test accounts) |
| 11 | +- **Data Access**: DEV_ prefixed collections (isolated dev data) |
| 12 | +- **Credentials**: Test accounts only (jamie@wewrite.app, test@wewrite.app, etc.) |
| 13 | +- **Trigger**: `NODE_ENV=development` AND `USE_DEV_AUTH=true` |
| 14 | + |
| 15 | +### 🔍 **Vercel Preview** |
| 16 | +- **Auth System**: Firebase Auth (production auth) |
| 17 | +- **Data Access**: Production collections (no DEV_ prefix) |
| 18 | +- **Credentials**: Real Firebase Auth accounts (jamiegray2234@gmail.com, etc.) |
| 19 | +- **Purpose**: Test with production data using real credentials |
| 20 | + |
| 21 | +### 🚀 **Vercel Production** |
| 22 | +- **Auth System**: Firebase Auth (production auth) |
| 23 | +- **Data Access**: Production collections (no DEV_ prefix) |
| 24 | +- **Credentials**: Real Firebase Auth accounts (jamiegray2234@gmail.com, etc.) |
| 25 | +- **Purpose**: Live production environment |
| 26 | + |
| 27 | +## Environment Detection Logic |
| 28 | + |
| 29 | +### Server-Side (API Routes) |
| 30 | +```typescript |
| 31 | +// ONLY use dev auth for local development |
| 32 | +const useDevAuth = process.env.NODE_ENV === 'development' && process.env.USE_DEV_AUTH === 'true'; |
| 33 | + |
| 34 | +if (useDevAuth) { |
| 35 | + // Use test accounts and dev auth system |
| 36 | +} else { |
| 37 | + // Use Firebase Auth with real credentials |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +### Client-Side (AuthProvider) |
| 42 | +```typescript |
| 43 | +// ONLY use dev auth for local development |
| 44 | +const useDevAuth = process.env.NODE_ENV === 'development' && process.env.NEXT_PUBLIC_USE_DEV_AUTH === 'true'; |
| 45 | + |
| 46 | +if (useDevAuth) { |
| 47 | + // Call /api/auth/login with test credentials |
| 48 | +} else { |
| 49 | + // Use Firebase Auth SDK with real credentials |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +## Data Access Patterns |
| 54 | + |
| 55 | +### Local Development |
| 56 | +- Collections: `DEV_users`, `DEV_pages`, `DEV_subscriptions` |
| 57 | +- Purpose: Isolated development data |
| 58 | +- Safety: Cannot affect production data |
| 59 | + |
| 60 | +### Preview & Production |
| 61 | +- Collections: `users`, `pages`, `subscriptions` (base names) |
| 62 | +- Purpose: Real production data |
| 63 | +- Safety: Use real credentials, real data |
| 64 | + |
| 65 | +## Test Credentials (Local Development Only) |
| 66 | + |
| 67 | +```typescript |
| 68 | +const testAccounts = [ |
| 69 | + { email: 'jamie@wewrite.app', password: 'TestPassword123!', isAdmin: true }, |
| 70 | + { email: 'test@wewrite.app', password: 'TestPassword123!', isAdmin: false }, |
| 71 | + { email: 'getwewrite@gmail.com', password: 'TestPassword123!', isAdmin: false }, |
| 72 | + { email: 'test@local.dev', password: 'TestPassword123!', isAdmin: false } |
| 73 | +]; |
| 74 | +``` |
| 75 | + |
| 76 | +**⚠️ These test credentials ONLY work in local development with dev auth enabled.** |
| 77 | + |
| 78 | +## Production Credentials (Preview & Production) |
| 79 | + |
| 80 | +Use real Firebase Auth accounts: |
| 81 | +- `jamiegray2234@gmail.com` (admin account) |
| 82 | +- Any other real Firebase Auth user accounts |
| 83 | + |
| 84 | +## Common Mistakes to Avoid |
| 85 | + |
| 86 | +### ❌ **WRONG: Using dev auth in preview** |
| 87 | +```typescript |
| 88 | +// DON'T DO THIS |
| 89 | +const isPreviewEnv = environmentType === 'preview'; |
| 90 | +const useDevAuth = isLocalDev || isPreviewEnv; // WRONG! |
| 91 | +``` |
| 92 | + |
| 93 | +### ✅ **CORRECT: Only dev auth in local development** |
| 94 | +```typescript |
| 95 | +// DO THIS |
| 96 | +const useDevAuth = process.env.NODE_ENV === 'development' && process.env.USE_DEV_AUTH === 'true'; |
| 97 | +``` |
| 98 | + |
| 99 | +### ❌ **WRONG: Expecting test credentials in preview** |
| 100 | +Preview environments should NOT accept `jamie@wewrite.app` or other test credentials. |
| 101 | + |
| 102 | +### ✅ **CORRECT: Use real credentials in preview** |
| 103 | +Preview environments should accept `jamiegray2234@gmail.com` and other real Firebase Auth accounts. |
| 104 | + |
| 105 | +## Debugging Authentication Issues |
| 106 | + |
| 107 | +### Check Environment Detection |
| 108 | +Visit: `/api/debug/auth-environment` |
| 109 | + |
| 110 | +Expected responses: |
| 111 | + |
| 112 | +**Local Development:** |
| 113 | +```json |
| 114 | +{ |
| 115 | + "environment": { "type": "development" }, |
| 116 | + "authConfiguration": { "useDevAuth": true, "authSystem": "dev-auth" }, |
| 117 | + "testCredentials": { "available": ["jamie@wewrite.app / TestPassword123!"] } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +**Preview/Production:** |
| 122 | +```json |
| 123 | +{ |
| 124 | + "environment": { "type": "preview" }, |
| 125 | + "authConfiguration": { "useDevAuth": false, "authSystem": "firebase-auth" }, |
| 126 | + "testCredentials": { "note": "Using Firebase Auth - use your production credentials" } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +## Environment Variables |
| 131 | + |
| 132 | +### Local Development |
| 133 | +```bash |
| 134 | +NODE_ENV=development |
| 135 | +USE_DEV_AUTH=true |
| 136 | +NEXT_PUBLIC_USE_DEV_AUTH=true |
| 137 | +``` |
| 138 | + |
| 139 | +### Preview/Production |
| 140 | +```bash |
| 141 | +NODE_ENV=production |
| 142 | +# USE_DEV_AUTH should be undefined or false |
| 143 | +# NEXT_PUBLIC_USE_DEV_AUTH should be undefined or false |
| 144 | +``` |
| 145 | + |
| 146 | +## File Locations |
| 147 | + |
| 148 | +### Authentication Logic |
| 149 | +- **Login**: `app/api/auth/login/route.ts` |
| 150 | +- **Session**: `app/api/auth/session/route.ts` |
| 151 | +- **Client**: `app/providers/AuthProvider.tsx` |
| 152 | + |
| 153 | +### Environment Detection |
| 154 | +- **Server**: `app/utils/environmentConfig.ts` |
| 155 | +- **Debug**: `app/api/debug/auth-environment/route.ts` |
| 156 | + |
| 157 | +## Summary |
| 158 | + |
| 159 | +**Remember: Preview environments are for testing with production data using real credentials, NOT for using test accounts with production data.** |
| 160 | + |
| 161 | +- **Local**: Dev auth + dev data + test credentials |
| 162 | +- **Preview**: Firebase auth + production data + real credentials |
| 163 | +- **Production**: Firebase auth + production data + real credentials |
0 commit comments