Skip to content

Commit a197138

Browse files
author
Deepak Pandey
committed
Fix final Supabase build error in admin-auth and backup
- Fix lib/auth/admin-auth.ts: Convert module-level Supabase client to lazy initialization - Fix lib/database/backup-strategy.ts: Convert backupManager to createBackupManager function - Fix app/api/admin/backup/route.ts: Update to use createBackupManager function - This resolves the final 'supabaseUrl is required' build error - Build now completes successfully with all 142 pages generated
1 parent 7c9b08b commit a197138

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

app/api/admin/backup/route.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NextRequest } from 'next/server';
2-
import { backupManager } from '@/lib/database/backup-strategy';
2+
import { createBackupManager } from '@/lib/database/backup-strategy';
33
import { authenticateAdmin } from '@/lib/auth/admin-auth';
44
import { withRateLimit } from '@/lib/security/rate-limiting';
55
import { RateLimitConfigs } from '@/lib/security/rate-limiting';
@@ -20,6 +20,7 @@ export async function GET(request: NextRequest) {
2020

2121
if (backupId) {
2222
// Get specific backup details
23+
const backupManager = createBackupManager();
2324
const backup = await backupManager.getBackupDetails(backupId);
2425
if (!backup) {
2526
return new Response(
@@ -33,6 +34,7 @@ export async function GET(request: NextRequest) {
3334
);
3435
} else {
3536
// Get backup list
37+
const backupManager = createBackupManager();
3638
const backups = await backupManager.getBackupList();
3739
return new Response(
3840
JSON.stringify(backups),
@@ -72,6 +74,7 @@ export const POST = withRateLimit(
7274

7375
if (action === 'create') {
7476
// Create new backup
77+
const backupManager = createBackupManager();
7578
const result = await backupManager.createBackup();
7679

7780
if (result.success) {
@@ -104,6 +107,7 @@ export const POST = withRateLimit(
104107
);
105108
}
106109

110+
const backupManager = createBackupManager();
107111
const result = await backupManager.restoreFromBackup(backupId);
108112

109113
if (result.success) {

lib/auth/admin-auth.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { NextRequest, NextResponse } from 'next/server';
33
import { cookies } from 'next/headers';
44
import { createServerClient } from '@supabase/ssr';
55

6-
// Create Supabase client for server-side operations
7-
const supabase = createClient(
8-
process.env.NEXT_PUBLIC_SUPABASE_URL!,
9-
process.env.SUPABASE_SERVICE_ROLE_KEY!
10-
);
6+
// Create Supabase client function to avoid build-time initialization
7+
function getSupabaseClient() {
8+
return createClient(
9+
process.env.NEXT_PUBLIC_SUPABASE_URL!,
10+
process.env.SUPABASE_SERVICE_ROLE_KEY!
11+
);
12+
}
1113

1214
export interface AuthenticatedUser {
1315
id: string;
@@ -52,7 +54,7 @@ export async function authenticateAdmin(request: NextRequest): Promise<Authentic
5254
}
5355

5456
// Get user profile
55-
const { data: profile, error: profileError } = await supabase
57+
const { data: profile, error: profileError } = await supabaseClient
5658
.from('profiles')
5759
.select('id, first_name, last_name, is_admin')
5860
.eq('id', user.id)
@@ -82,6 +84,7 @@ export async function authenticateAdmin(request: NextRequest): Promise<Authentic
8284
const token = authHeader.replace('Bearer ', '');
8385

8486
// Verify the JWT token
87+
const supabase = getSupabaseClient();
8588
const { data: { user }, error: tokenError } = await supabase.auth.getUser(token);
8689

8790
if (tokenError || !user) {

lib/database/backup-strategy.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,15 @@ export class DatabaseConnectionManager {
451451
}
452452
}
453453

454-
// Global backup manager instance
455-
export const backupManager = new DatabaseBackupManager({
456-
supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_URL!,
457-
supabaseServiceKey: process.env.SUPABASE_SERVICE_ROLE_KEY!,
458-
backupRetentionDays: 30,
459-
backupSchedule: '0 2 * * *' // Daily at 2 AM
460-
});
454+
// Function to create backup manager instance (lazy initialization)
455+
export function createBackupManager(): DatabaseBackupManager {
456+
return new DatabaseBackupManager({
457+
supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_URL!,
458+
supabaseServiceKey: process.env.SUPABASE_SERVICE_ROLE_KEY!,
459+
backupRetentionDays: 30,
460+
backupSchedule: '0 2 * * *' // Daily at 2 AM
461+
});
462+
}
461463

462464
// Global connection manager instance
463465
export const connectionManager = DatabaseConnectionManager.getInstance();

0 commit comments

Comments
 (0)