diff --git a/app/api/courrier/route.ts b/app/api/courrier/route.ts index e5ab7b8b..bd03a8de 100644 --- a/app/api/courrier/route.ts +++ b/app/api/courrier/route.ts @@ -77,8 +77,10 @@ export async function GET(request: Request) { envelope: true, flags: true, bodyStructure: true, - source: true, // Get the full email source - bodyParts: ['text/plain', 'text/html'] // Get both text and HTML content + bodyParts: [ + 'text/plain', + 'text/html' + ] }); for await (const message of messages) { @@ -86,7 +88,8 @@ export async function GET(request: Request) { let content = ''; if (message.bodyParts) { // Prefer HTML content if available - content = message.bodyParts.get('text/html')?.toString() || message.bodyParts.get('text/plain')?.toString() || ''; + content = message.bodyParts.get('text/html')?.toString() || + message.bodyParts.get('text/plain')?.toString() || ''; } result.push({ @@ -101,8 +104,7 @@ export async function GET(request: Request) { folder: mailbox.path, hasAttachments: message.bodyStructure?.type === 'multipart', flags: Array.from(message.flags), - content: content, - source: message.source?.toString() || '' // Include full email source for parsing + content: content }); } diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 5ecad122..93035c19 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -500,22 +500,32 @@ export default function CourrierPage() { try { console.log('Checking for stored credentials...'); const response = await fetch('/api/courrier'); + const data = await response.json(); + if (!response.ok) { - const errorData = await response.json(); - console.log('API response error:', errorData); - if (errorData.error === 'No stored credentials found') { + console.log('API response error:', data); + if (data.error === 'No mail credentials found. Please configure your email account.') { console.log('No credentials found, redirecting to login...'); router.push('/courrier/login'); return; } - throw new Error(errorData.error || 'Failed to check credentials'); + if (data.error === 'Unauthorized') { + console.log('User not authenticated, redirecting to auth...'); + router.push('/api/auth/signin'); + return; + } + setError(data.error || 'Failed to check credentials'); + setLoading(false); + return; } + + // If we get here, credentials are valid and we have emails console.log('Credentials verified, loading emails...'); setLoading(false); loadEmails(); } catch (err) { console.error('Error checking credentials:', err); - setError(err instanceof Error ? err.message : 'Failed to check credentials'); + setError('Failed to connect to mail server. Please try again later.'); setLoading(false); } };