Skip to content

Commit 0db4c11

Browse files
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

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

app/api/admin/analytics-data/route.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ export async function GET(request: NextRequest) {
1010
return NextResponse.json({ error: permissionCheck.error }, { status: 401 });
1111
}
1212

13-
const { adminDb } = getFirebaseAdmin();
13+
const admin = getFirebaseAdmin();
14+
if (!admin) {
15+
return NextResponse.json({ error: 'Firebase Admin not available' }, { status: 500 });
16+
}
17+
const adminDb = admin.firestore();
1418

1519
const { searchParams } = new URL(request.url);
1620
const type = searchParams.get('type');

app/api/auth/login/route.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ export async function POST(request: NextRequest) {
117117
}
118118

119119
// Production mode: use Firebase Auth
120-
const { auth, firestore } = getFirebaseAdmin();
120+
const admin = getFirebaseAdmin();
121+
if (!admin) {
122+
return NextResponse.json({ error: 'Firebase Admin not available' }, { status: 500 });
123+
}
124+
const auth = admin.auth();
125+
const firestore = admin.firestore();
121126

122127
// Determine if input is email or username
123128
const isEmail = emailOrUsername.includes('@');

app/api/dev/setup-test-allocations/route.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import { getFirebaseAdmin } from '../../../firebase/admin';
1010
import { getCollectionName } from '../../../utils/environmentConfig';
1111
import { DEV_TEST_USERS } from "../../../utils/testUsers";
1212

13-
const { db } = getFirebaseAdmin();
13+
const admin = getFirebaseAdmin();
14+
if (!admin) {
15+
throw new Error('Firebase Admin not available');
16+
}
17+
const db = admin.firestore();
1418

1519
// Check if development auth is active
1620
const isDevelopmentAuthActive = () => {

app/api/subscription/sync-status/route.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ export async function POST(request: NextRequest) {
2121

2222
console.log(`[SUBSCRIPTION SYNC] Syncing subscription status for user ${userId}`);
2323

24-
const { adminDb } = getFirebaseAdmin();
24+
const admin = getFirebaseAdmin();
25+
if (!admin) {
26+
console.error('[SUBSCRIPTION SYNC] Firebase Admin not available');
27+
return NextResponse.json({ error: 'Firebase Admin not available' }, { status: 500 });
28+
}
29+
30+
const adminDb = admin.firestore();
2531
const { parentPath, subCollectionName } = getSubCollectionPath(
2632
PAYMENT_COLLECTIONS.USERS,
2733
userId,

0 commit comments

Comments
 (0)