(function () { "use strict"; var $ = window.MailSharp; $.apiFetch("api/auth/state").then(function (authState) { if (authState.isLoggedIn) { window.location.href = "./"; return; } return Promise.all([ $.apiFetch("api/language/state"), $.apiFetch("api/auth/login-defaults") ]).then(function (results) { var langState = results[0]; var defaults = results[1]; $.applyStrings(langState.strings); document.documentElement.lang = langState.current; document.getElementById("ImapHost").value = defaults.imapHost; document.getElementById("SmtpHost").value = defaults.smtpHost; document.getElementById("ImapPort").value = defaults.imapPort; document.getElementById("SmtpPort").value = defaults.smtpPort; document.getElementById("ImapSecurity").value = defaults.imapSecurity; document.getElementById("SmtpSecurity").value = defaults.smtpSecurity; }); }); document.getElementById("advancedToggle").addEventListener("click", function (e) { e.preventDefault(); document.getElementById("advancedBox").classList.toggle("show"); }); document.getElementById("loginForm").addEventListener("submit", function (e) { e.preventDefault(); var form = e.target; var fd = new FormData(form); var advancedCheckbox = form.querySelector('input[name="Advanced"]'); var payload = { emailAddress: fd.get("EmailAddress") || "", username: fd.get("Username") || "", password: fd.get("Password") || "", advanced: advancedCheckbox ? advancedCheckbox.checked : false, imapHost: fd.get("ImapHost") || "", imapPort: parseInt(fd.get("ImapPort"), 10) || 0, imapSecurity: fd.get("ImapSecurity") || "SslTls", smtpHost: fd.get("SmtpHost") || "", smtpPort: parseInt(fd.get("SmtpPort"), 10) || 0, smtpSecurity: fd.get("SmtpSecurity") || "StartTls" }; var errorBox = document.getElementById("loginError"); errorBox.style.display = "none"; $.apiFetch("api/auth/login", { method: "POST", body: payload }) .then(function () { window.location.href = "./"; }) .catch(function (err) { errorBox.textContent = err.message; errorBox.style.display = ""; }); }); })();