From 654b0587febb98b950f217a68350dc81f5ed1027 Mon Sep 17 00:00:00 2001 From: NotII <46204250+NotII@users.noreply.github.com> Date: Sun, 23 Mar 2025 22:21:22 +0000 Subject: [PATCH] Update route.ts --- app/api/auth/check/route.ts | 76 +++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/app/api/auth/check/route.ts b/app/api/auth/check/route.ts index 0a6fe99..351c3ca 100644 --- a/app/api/auth/check/route.ts +++ b/app/api/auth/check/route.ts @@ -26,44 +26,54 @@ export async function GET(req: NextRequest) { const apiUrl = process.env.SERVER_API_URL || 'https://internal-api.inboxi.ng/api'; console.log(`Auth check: Calling external API: ${apiUrl}/auth/me`); - const res = await fetch(`${apiUrl}/auth/me`, { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${token}` - }, - cache: 'no-store' - }); - - console.log('Auth check: External API response status:', res.status); - - if (!res.ok) { - try { - const errorData = await res.json(); - console.log('Auth check failed:', { - status: res.status, - statusText: res.statusText, - body: errorData - }); - } catch { - const errorText = await res.text().catch(() => 'No response body'); - console.log('Auth check failed:', { - status: res.status, - statusText: res.statusText, - body: errorText - }); + try { + const res = await fetch(`${apiUrl}/auth/me`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${token}` + }, + cache: 'no-store' + }); + + console.log('Auth check: External API response status:', res.status); + + if (!res.ok) { + try { + const errorData = await res.json(); + console.log('Auth check failed:', { + status: res.status, + statusText: res.statusText, + body: errorData + }); + } catch { + const errorText = await res.text().catch(() => 'No response body'); + console.log('Auth check failed:', { + status: res.status, + statusText: res.statusText, + body: errorText + }); + } + + return NextResponse.json( + { error: 'Authentication failed', details: res.statusText }, + { status: res.status } + ); } + const data = await res.json(); + console.log('Auth check succeeded:', { userId: data._id || 'unknown' }); + return NextResponse.json(data); + } catch (fetchError) { + console.error('Auth check network error:', fetchError); return NextResponse.json( - { error: 'Authentication failed', details: res.statusText }, - { status: res.status } + { + error: 'Failed to connect to authentication service', + details: fetchError instanceof Error ? fetchError.message : String(fetchError) + }, + { status: 503 } // Service Unavailable ); } - - const data = await res.json(); - console.log('Auth check succeeded:', { userId: data._id || 'unknown' }); - return NextResponse.json(data); - } catch (error) { console.error('Auth check error:', error); return NextResponse.json(