// Utility: Save and load credentials function saveCredentials(username, password) { localStorage.setItem('username', username); localStorage.setItem('password', password); } function getCredentials() { return { username: localStorage.getItem('username'), password: localStorage.getItem('password') }; } function clearCredentials() { localStorage.removeItem('username'); localStorage.removeItem('password'); } function logout() { clearCredentials(); window.location.href = 'login.html'; } // Registration if (document.getElementById('registerForm')) { document.getElementById('registerForm').onsubmit = async function(e) { e.preventDefault(); const username = document.getElementById('registerUsername').value; const password = document.getElementById('registerPassword').value; const res = await fetch('http://localhost:8080/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }) }); if (res.ok) { saveCredentials(username, password); window.location.href = 'inbox.html'; } else { const err = await res.text(); document.getElementById('registerError').innerText = err; } }; } // Login if (document.getElementById('loginForm')) { document.getElementById('loginForm').onsubmit = async function(e) { e.preventDefault(); const username = document.getElementById('loginUsername').value; const password = document.getElementById('loginPassword').value; // Try to fetch mailbox to verify credentials const res = await fetch(`http://localhost:8080/mailbox?user=${encodeURIComponent(username)}`, { headers: { 'Authorization': 'Basic ' + btoa(username + ':' + password) } }); if (res.ok) { saveCredentials(username, password); window.location.href = 'inbox.html'; } else { document.getElementById('loginError').innerText = 'Invalid username or password.'; } }; } // Inbox if (document.getElementById('inboxList')) { const { username, password } = getCredentials(); if (!username || !password) { window.location.href = 'login.html'; } else { fetch(`http://localhost:8080/mailbox?user=${encodeURIComponent(username)}`, { headers: { 'Authorization': 'Basic ' + btoa(username + ':' + password) } }) .then(res => res.json()) .then(emails => { if (!Array.isArray(emails)) throw new Error('Invalid mailbox response'); // Determine current user's full address (username!domain if possible) // Try to find domain from any received or sent email let userDomain = null; for (const email of emails) { if (email.to === username && email.domain) { userDomain = email.domain; break; } if (email.from === username && email.domain) { userDomain = email.domain; break; } } const userFull = userDomain ? `${username}!${userDomain}` : username; // Inbox: emails where 'to!domain' matches current user const inboxEmails = emails.filter(email => { if (!email.to) return false; const toFull = email.domain ? `${email.to}!${email.domain}` : email.to; return toFull === userFull; }); // Sent: emails where 'from!domain' matches current user const sentEmails = emails.filter(email => { if (!email.from) return false; const fromFull = email.domain ? `${email.from}!${email.domain}` : email.from; return fromFull === userFull; }); let html = ''; html += '
No emails.
'; } else { html += inboxEmails.map(email => { const fromDisplay = email.domain ? `${email.from}!${email.domain}` : email.from; return `No sent emails.
'; } else { html += sentEmails.map(email => { const toDisplay = email.domain ? `${email.to}!${email.domain}` : email.to; return `