Code · 71 lines · 2157 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71(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 = "";
			});
	});
})();