diff --git a/app/api/emails/route.ts b/app/api/emails/route.ts
index 3e30fe5c..46a29acd 100644
--- a/app/api/emails/route.ts
+++ b/app/api/emails/route.ts
@@ -19,24 +19,54 @@ export async function GET(req: NextRequest) {
);
}
- // Try to access the Mail app through WebDAV
- const response = await fetch(`${nextcloudUrl}/remote.php/dav/`, {
- method: 'PROPFIND',
+ // First, try to get the user's Nextcloud ID using the OIDC token
+ const userInfoResponse = await fetch(`${nextcloudUrl}/ocs/v2.php/cloud/user`, {
headers: {
'Authorization': `Bearer ${session.accessToken}`,
- 'Depth': '1',
- 'Content-Type': 'application/xml',
+ 'Accept': 'application/json',
+ 'OCS-APIRequest': 'true',
+ },
+ });
+
+ if (!userInfoResponse.ok) {
+ console.error('Failed to get user info:', {
+ status: userInfoResponse.status,
+ statusText: userInfoResponse.statusText,
+ url: userInfoResponse.url
+ });
+
+ if (userInfoResponse.status === 401) {
+ return NextResponse.json({ error: 'Nextcloud authentication failed' }, { status: 401 });
+ }
+
+ return NextResponse.json(
+ { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
+ { status: 404 }
+ );
+ }
+
+ const userInfo = await userInfoResponse.json();
+ const userId = userInfo?.ocs?.data?.id;
+
+ if (!userId) {
+ console.error('Failed to get user ID from Nextcloud');
+ return NextResponse.json(
+ { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
+ { status: 404 }
+ );
+ }
+
+ // Now try to access the Mail app
+ const response = await fetch(`${nextcloudUrl}/ocs/v2.php/apps/mail/api/v1/accounts`, {
+ headers: {
+ 'Authorization': `Bearer ${session.accessToken}`,
+ 'Accept': 'application/json',
+ 'OCS-APIRequest': 'true',
},
- body: `
-
-
-
-
- `
});
if (!response.ok) {
- console.error('WebDAV access failed:', {
+ console.error('Mail app check failed:', {
status: response.status,
statusText: response.statusText,
url: response.url
@@ -52,57 +82,7 @@ export async function GET(req: NextRequest) {
);
}
- // If WebDAV access works, try to access mail
- const mailResponse = await fetch(`${nextcloudUrl}/remote.php/dav/principals/users/${session.user.username}/mail/`, {
- method: 'PROPFIND',
- headers: {
- 'Authorization': `Bearer ${session.accessToken}`,
- 'Depth': '1',
- 'Content-Type': 'application/xml',
- },
- body: `
-
-
-
-
- `
- });
-
- if (!mailResponse.ok) {
- console.error('Mail access failed:', {
- status: mailResponse.status,
- statusText: mailResponse.statusText,
- url: mailResponse.url
- });
- return NextResponse.json(
- { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
- { status: 404 }
- );
- }
-
- // If we get here, the Mail app is available but we need to use a different endpoint
- const messagesResponse = await fetch(`${nextcloudUrl}/index.php/apps/mail/api/accounts`, {
- headers: {
- 'Authorization': `Bearer ${session.accessToken}`,
- 'Accept': 'application/json',
- 'Content-Type': 'application/json',
- },
- });
-
- if (!messagesResponse.ok) {
- console.error('Mail accounts fetch failed:', {
- status: messagesResponse.status,
- statusText: messagesResponse.statusText,
- url: messagesResponse.url
- });
- return NextResponse.json(
- { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." },
- { status: 404 }
- );
- }
-
- const accounts = await messagesResponse.json();
-
+ // If we get here, the Mail app is available
// For now, return a success response with an empty array
// We'll implement the actual message fetching once we confirm the correct endpoint
return NextResponse.json([]);