Skip to content

Commit 7352051

Browse files
committed
Fix admin authentication and add comprehensive admin debug endpoint
Fixed: - Admin auth helper Firebase Admin destructuring issue (same as session route) - Added comprehensive admin test endpoint to debug data pipeline issues The admin test endpoint will help identify exactly which part of the data pipeline is failing: 1. Admin permission check 2. Collection name resolution 3. Analytics service data fetching 4. Firebase/Firestore connectivity This will help us systematically fix all the 'No data available' issues in the admin dashboard.
1 parent 742aae6 commit 7352051

File tree

2 files changed

+140
-1
lines changed

2 files changed

+140
-1
lines changed

app/api/admin-auth-helper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export async function checkAdminPermissions(request: NextRequest): Promise<{succ
3434

3535
// Get user email using Firebase Admin SDK
3636
const admin = getFirebaseAdmin();
37-
const userRecord = await admin!.auth().getUser(userId);
37+
const adminAuth = admin.auth();
38+
const userRecord = await adminAuth.getUser(userId);
3839
const userEmail = userRecord.email;
3940

4041
if (!userEmail) {

app/api/debug/admin-test/route.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { NextRequest, NextResponse } from 'next/server';
2+
import { checkAdminPermissions } from '../../admin-auth-helper';
3+
import { DashboardAnalyticsService } from '../../../services/dashboardAnalytics';
4+
import { getCollectionName } from '../../../utils/environmentConfig';
5+
6+
/**
7+
* Debug endpoint to test admin authentication and data access
8+
*/
9+
export async function GET(request: NextRequest) {
10+
try {
11+
console.log('[Admin Test] Starting admin test...');
12+
13+
const testResults = {
14+
timestamp: new Date().toISOString(),
15+
steps: []
16+
};
17+
18+
// Step 1: Check admin permissions
19+
try {
20+
const adminCheck = await checkAdminPermissions(request);
21+
testResults.steps.push({
22+
step: 1,
23+
name: 'Admin Permission Check',
24+
success: adminCheck.success,
25+
data: adminCheck.success ? { userEmail: adminCheck.userEmail } : { error: adminCheck.error }
26+
});
27+
28+
if (!adminCheck.success) {
29+
return NextResponse.json(testResults, { status: 200 });
30+
}
31+
} catch (error) {
32+
testResults.steps.push({
33+
step: 1,
34+
name: 'Admin Permission Check',
35+
success: false,
36+
error: error.message
37+
});
38+
return NextResponse.json(testResults, { status: 200 });
39+
}
40+
41+
// Step 2: Test collection names
42+
try {
43+
const collections = {
44+
users: getCollectionName('users'),
45+
pages: getCollectionName('pages'),
46+
analytics_events: getCollectionName('analytics_events'),
47+
subscriptions: getCollectionName('subscriptions')
48+
};
49+
50+
testResults.steps.push({
51+
step: 2,
52+
name: 'Collection Names',
53+
success: true,
54+
data: collections
55+
});
56+
} catch (error) {
57+
testResults.steps.push({
58+
step: 2,
59+
name: 'Collection Names',
60+
success: false,
61+
error: error.message
62+
});
63+
}
64+
65+
// Step 3: Test simple analytics query
66+
try {
67+
const dateRange = {
68+
startDate: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000), // 7 days ago
69+
endDate: new Date()
70+
};
71+
72+
console.log('[Admin Test] Testing accounts analytics with date range:', dateRange);
73+
const accountsData = await DashboardAnalyticsService.getNewAccountsCreated(dateRange, 10);
74+
75+
testResults.steps.push({
76+
step: 3,
77+
name: 'Accounts Analytics Test',
78+
success: true,
79+
data: {
80+
dateRange,
81+
dataLength: accountsData.length,
82+
sampleData: accountsData.slice(0, 3)
83+
}
84+
});
85+
} catch (error) {
86+
testResults.steps.push({
87+
step: 3,
88+
name: 'Accounts Analytics Test',
89+
success: false,
90+
error: error.message,
91+
stack: error.stack
92+
});
93+
}
94+
95+
// Step 4: Test pages analytics
96+
try {
97+
const dateRange = {
98+
startDate: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
99+
endDate: new Date()
100+
};
101+
102+
const pagesData = await DashboardAnalyticsService.getNewPagesCreated(dateRange, 10);
103+
104+
testResults.steps.push({
105+
step: 4,
106+
name: 'Pages Analytics Test',
107+
success: true,
108+
data: {
109+
dataLength: pagesData.length,
110+
sampleData: pagesData.slice(0, 3)
111+
}
112+
});
113+
} catch (error) {
114+
testResults.steps.push({
115+
step: 4,
116+
name: 'Pages Analytics Test',
117+
success: false,
118+
error: error.message
119+
});
120+
}
121+
122+
return NextResponse.json(testResults, {
123+
status: 200,
124+
headers: {
125+
'Cache-Control': 'no-cache, no-store, must-revalidate'
126+
}
127+
});
128+
129+
} catch (error) {
130+
console.error('[Admin Test] Error:', error);
131+
132+
return NextResponse.json({
133+
error: 'Admin test failed',
134+
details: error instanceof Error ? error.message : 'Unknown error',
135+
timestamp: new Date().toISOString()
136+
}, { status: 500 });
137+
}
138+
}

0 commit comments

Comments
 (0)