mail page imap connection mime 5 bis rest 16 login page 19

This commit is contained in:
alma 2025-04-16 00:37:17 +02:00
parent 925e321759
commit 2fdebf82e1
3 changed files with 33 additions and 47 deletions

View File

@ -71,6 +71,12 @@ function getStoredCredentials(): StoredCredentials | null {
password: '***' password: '***'
}); });
// Validate required fields
if (!credentials.email || !credentials.password || !credentials.host || !credentials.port) {
console.error('Missing required credentials fields');
return null;
}
return { return {
email: credentials.email, email: credentials.email,
password: credentials.password, password: credentials.password,

View File

@ -56,17 +56,23 @@ export default function LoginPage() {
password: '***' password: '***'
}); });
// Store as a single cookie // Store as a single cookie with proper options
setCookie('imap_credentials', JSON.stringify(credentials), { setCookie('imap_credentials', JSON.stringify(credentials), {
maxAge: 60 * 60 * 24, // 1 day maxAge: 60 * 60 * 24, // 1 day
path: '/', path: '/',
sameSite: 'lax' sameSite: 'lax',
secure: process.env.NODE_ENV === 'production',
httpOnly: false // Allow access from JavaScript
}); });
// Verify cookie was set // Verify cookie was set
const stored = document.cookie.split(';').find(c => c.trim().startsWith('imap_credentials=')); const stored = document.cookie.split(';').find(c => c.trim().startsWith('imap_credentials='));
console.log('Cookie verification:', stored ? 'Cookie found' : 'Cookie not found'); console.log('Cookie verification:', stored ? 'Cookie found' : 'Cookie not found');
if (!stored) {
throw new Error('Failed to store credentials');
}
// Redirect to mail page // Redirect to mail page
router.push('/mail'); router.push('/mail');
} catch (err) { } catch (err) {

View File

@ -430,63 +430,37 @@ export default function MailPage() {
// Fetch emails from IMAP API // Fetch emails from IMAP API
const loadEmails = async () => { const loadEmails = async () => {
setLoading(true);
setError(null);
try { try {
console.log('Starting to load emails...');
setLoading(true);
setError(null);
const response = await fetch('/api/mail'); const response = await fetch('/api/mail');
console.log('API response status:', response.status);
if (!response.ok) { if (!response.ok) {
const errorData = await response.json(); const errorData = await response.json();
if (errorData.error === 'Invalid login or password') { console.error('API error:', errorData);
// Clear invalid credentials and redirect to login throw new Error(errorData.error || 'Failed to load emails');
localStorage.removeItem('imapCredentials');
router.push('/mail/login');
return;
}
throw new Error(errorData.details || 'Failed to fetch emails');
} }
const data = await response.json(); const data = await response.json();
console.log('API Response:', data); // Debug log console.log('Received emails:', data.length);
// Handle different possible response formats // Ensure all dates are Date objects
let emailData = []; const processedEmails = data.map((email: Email) => ({
if (Array.isArray(data)) { ...email,
emailData = data; date: new Date(email.date),
} 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(),
read: email.read || false, 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); setEmails(processedEmails);
console.log('Emails loaded successfully');
} catch (err) { } catch (err) {
console.error('Error fetching emails:', err); console.error('Error loading emails:', err);
setError(err instanceof Error ? err.message : 'An error occurred'); setError(err instanceof Error ? err.message : 'Failed to load emails');
setEmails([]);
} finally { } finally {
setLoading(false); setLoading(false);
} }