diff --git a/app/api/emails/route.ts b/app/api/emails/route.ts index 3bd45b3d..b4ccdf89 100644 --- a/app/api/emails/route.ts +++ b/app/api/emails/route.ts @@ -6,7 +6,7 @@ export async function GET(req: NextRequest) { try { const session = await getServerSession(authOptions); - if (!session?.user?.email) { + if (!session?.user?.email || !session?.accessToken) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } @@ -19,22 +19,24 @@ export async function GET(req: NextRequest) { ); } - // Try to access the Mail app directly + // Try to access the Mail app using the Keycloak token const response = await fetch(`${nextcloudUrl}/ocs/v2.php/apps/mail/api/v1/accounts`, { headers: { 'Accept': 'application/json', 'OCS-APIRequest': 'true', 'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest', + 'Authorization': `Bearer ${session.accessToken}`, }, }); + const responseText = await response.text(); let responseData; + try { - responseData = await response.json(); + responseData = JSON.parse(responseText); } catch (error) { - const text = await response.text(); - console.error('Failed to parse response:', text); + console.error('Failed to parse response:', responseText); return NextResponse.json({ error: 'Invalid response format' }, { status: 500 }); }