Update route.ts

This commit is contained in:
NotII
2025-03-23 22:21:22 +00:00
parent 991593c301
commit 654b0587fe

View File

@@ -26,44 +26,54 @@ export async function GET(req: NextRequest) {
const apiUrl = process.env.SERVER_API_URL || 'https://internal-api.inboxi.ng/api'; const apiUrl = process.env.SERVER_API_URL || 'https://internal-api.inboxi.ng/api';
console.log(`Auth check: Calling external API: ${apiUrl}/auth/me`); console.log(`Auth check: Calling external API: ${apiUrl}/auth/me`);
const res = await fetch(`${apiUrl}/auth/me`, { try {
method: 'GET', const res = await fetch(`${apiUrl}/auth/me`, {
headers: { method: 'GET',
'Content-Type': 'application/json', headers: {
'Authorization': `Bearer ${token}` 'Content-Type': 'application/json',
}, 'Authorization': `Bearer ${token}`
cache: 'no-store' },
}); cache: 'no-store'
});
console.log('Auth check: External API response status:', res.status); console.log('Auth check: External API response status:', res.status);
if (!res.ok) { if (!res.ok) {
try { try {
const errorData = await res.json(); const errorData = await res.json();
console.log('Auth check failed:', { console.log('Auth check failed:', {
status: res.status, status: res.status,
statusText: res.statusText, statusText: res.statusText,
body: errorData body: errorData
}); });
} catch { } catch {
const errorText = await res.text().catch(() => 'No response body'); const errorText = await res.text().catch(() => 'No response body');
console.log('Auth check failed:', { console.log('Auth check failed:', {
status: res.status, status: res.status,
statusText: res.statusText, statusText: res.statusText,
body: errorText 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( 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) { } catch (error) {
console.error('Auth check error:', error); console.error('Auth check error:', error);
return NextResponse.json( return NextResponse.json(