diff --git a/app/api/mail/mark-read/route.ts b/app/api/mail/mark-read/route.ts index 2527400d..ecdca57a 100644 --- a/app/api/mail/mark-read/route.ts +++ b/app/api/mail/mark-read/route.ts @@ -8,7 +8,9 @@ export async function POST(request: Request) { let client: ImapFlow | null = null; try { const session = await getServerSession(authOptions); + if (!session?.user?.id) { + console.error('No session or user ID found'); return NextResponse.json( { error: 'Unauthorized' }, { status: 401 } @@ -18,6 +20,7 @@ export async function POST(request: Request) { const { emailId, isRead } = await request.json(); if (!emailId || typeof isRead !== 'boolean') { + console.error('Invalid request parameters:', { emailId, isRead }); return NextResponse.json( { error: 'Invalid request parameters' }, { status: 400 } @@ -29,7 +32,16 @@ export async function POST(request: Request) { const folder = url.searchParams.get('folder') || 'INBOX'; try { + // Initialize IMAP client with user credentials client = await getImapClient(); + if (!client) { + console.error('Failed to initialize IMAP client'); + return NextResponse.json( + { error: 'Failed to initialize email client' }, + { status: 500 } + ); + } + await client.connect(); await client.mailboxOpen(folder); @@ -40,6 +52,7 @@ export async function POST(request: Request) { }); if (!message) { + console.error('Email not found:', emailId); return NextResponse.json( { error: 'Email not found' }, { status: 404 }