diff --git a/app/api/mail/route.ts b/app/api/mail/route.ts index 1435ca0..4099a3b 100644 --- a/app/api/mail/route.ts +++ b/app/api/mail/route.ts @@ -71,6 +71,12 @@ function getStoredCredentials(): StoredCredentials | null { password: '***' }); + // Validate required fields + if (!credentials.email || !credentials.password || !credentials.host || !credentials.port) { + console.error('Missing required credentials fields'); + return null; + } + return { email: credentials.email, password: credentials.password, diff --git a/app/mail/login/page.tsx b/app/mail/login/page.tsx index 9f1e34c..2867a7e 100644 --- a/app/mail/login/page.tsx +++ b/app/mail/login/page.tsx @@ -56,17 +56,23 @@ export default function LoginPage() { password: '***' }); - // Store as a single cookie + // Store as a single cookie with proper options setCookie('imap_credentials', JSON.stringify(credentials), { maxAge: 60 * 60 * 24, // 1 day path: '/', - sameSite: 'lax' + sameSite: 'lax', + secure: process.env.NODE_ENV === 'production', + httpOnly: false // Allow access from JavaScript }); // Verify cookie was set const stored = document.cookie.split(';').find(c => c.trim().startsWith('imap_credentials=')); console.log('Cookie verification:', stored ? 'Cookie found' : 'Cookie not found'); + if (!stored) { + throw new Error('Failed to store credentials'); + } + // Redirect to mail page router.push('/mail'); } catch (err) { diff --git a/app/mail/page.tsx b/app/mail/page.tsx index d562486..e51c1a3 100644 --- a/app/mail/page.tsx +++ b/app/mail/page.tsx @@ -430,63 +430,37 @@ export default function MailPage() { // Fetch emails from IMAP API const loadEmails = async () => { - setLoading(true); - setError(null); try { + console.log('Starting to load emails...'); + setLoading(true); + setError(null); + const response = await fetch('/api/mail'); + console.log('API response status:', response.status); + if (!response.ok) { const errorData = await response.json(); - if (errorData.error === 'Invalid login or password') { - // Clear invalid credentials and redirect to login - localStorage.removeItem('imapCredentials'); - router.push('/mail/login'); - return; - } - throw new Error(errorData.details || 'Failed to fetch emails'); + console.error('API error:', errorData); + throw new Error(errorData.error || 'Failed to load emails'); } + const data = await response.json(); - console.log('API Response:', data); // Debug log + console.log('Received emails:', data.length); - // Handle different possible response formats - let emailData = []; - if (Array.isArray(data)) { - emailData = data; - } else if (data && typeof data === 'object') { - // If data is an object, try to extract emails from it - if (data.emails) { - emailData = data.emails; - } else if (data.messages) { - emailData = data.messages; - } else { - // Try to convert object values to array - emailData = Object.values(data); - } - } - - console.log('Processed email data:', emailData); // Debug log - - // Ensure all dates are Date objects and add required properties - const processedEmails = emailData.map((email: any) => ({ - id: email.id || email.uid || email.messageId || Math.random().toString(), - accountId: email.accountId || '1', - from: email.from || '', - fromName: email.fromName || email.from?.split('@')[0] || '', - to: email.to || '', - subject: email.subject || '(No Subject)', - body: email.body || email.text || email.html || '', - preview: email.preview || email.body?.substring(0, 100) || '', - category: email.category || 'inbox', - date: email.date ? new Date(email.date) : new Date(), + // Ensure all dates are Date objects + const processedEmails = data.map((email: Email) => ({ + ...email, + date: new Date(email.date), read: email.read || false, - starred: email.starred || false + starred: email.starred || false, + category: email.category || 'inbox' })); - console.log('Final processed emails:', processedEmails); // Debug log setEmails(processedEmails); + console.log('Emails loaded successfully'); } catch (err) { - console.error('Error fetching emails:', err); - setError(err instanceof Error ? err.message : 'An error occurred'); - setEmails([]); + console.error('Error loading emails:', err); + setError(err instanceof Error ? err.message : 'Failed to load emails'); } finally { setLoading(false); }