Commit 0db4c11
committed
🔧 FIREBASE ADMIN FIXES: Fixed subscription sync-status and other API routes
PROBLEM: Preview environment subscription sync failing with 'Cannot read properties of undefined (reading 'doc')'
ROOT CAUSE: Multiple API routes incorrectly destructuring from getFirebaseAdmin()
SOLUTION: Fixed Firebase Admin initialization pattern across all affected routes
KEY CHANGES:
�� Fixed getFirebaseAdmin() Usage Pattern:
❌ WRONG: const { adminDb } = getFirebaseAdmin();
✅ CORRECT: const admin = getFirebaseAdmin(); const adminDb = admin.firestore();
📍 Fixed Routes:
✅ app/api/subscription/sync-status/route.ts - Main issue causing preview errors
✅ app/api/admin/analytics-data/route.ts - Same destructuring bug
✅ app/api/auth/login/route.ts - Fixed auth and firestore destructuring
✅ app/api/dev/setup-test-allocations/route.ts - Fixed db destructuring
🛡️ Added Null Checks:
✅ Added proper null checking for Firebase Admin availability
✅ Return 500 error when Firebase Admin not available
✅ Consistent error handling across all routes
TECHNICAL DETAILS:
🔍 Root Cause Analysis:
- getFirebaseAdmin() returns admin instance directly (typeof admin)
- Routes were trying to destructure { adminDb, auth, firestore, db }
- This resulted in undefined values causing 'Cannot read properties' errors
- Preview environment was particularly affected due to initialization timing
🎯 Correct Pattern:
🔧 Environment Impact:
- Preview environment now properly handles Firebase Admin initialization
- Production environment maintains existing functionality
- Development environment unaffected (was working correctly)
BENEFITS:
✅ Subscription sync-status now works in preview environment
✅ All Firebase Admin API routes use consistent initialization pattern
✅ Better error handling when Firebase Admin unavailable
✅ Prevents similar issues in future API routes
This fixes the preview environment subscription errors and ensures robust Firebase Admin usage! 🎉🔧1 parent ec6e803 commit 0db4c11
4 files changed
+23
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
14 | 18 | | |
15 | 19 | | |
16 | 20 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
120 | | - | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
121 | 126 | | |
122 | 127 | | |
123 | 128 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
14 | 18 | | |
15 | 19 | | |
16 | 20 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
25 | 31 | | |
26 | 32 | | |
27 | 33 | | |
| |||
0 commit comments