summary refs log tree commit diff
path: root/src-slowcord/login/public/js/handler.js
diff options
context:
space:
mode:
Diffstat (limited to 'src-slowcord/login/public/js/handler.js')
-rw-r--r--src-slowcord/login/public/js/handler.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/src-slowcord/login/public/js/handler.js b/src-slowcord/login/public/js/handler.js
new file mode 100644
index 00000000..68a656b4
--- /dev/null
+++ b/src-slowcord/login/public/js/handler.js
@@ -0,0 +1,41 @@
+const handleSubmit = async (path, body) => {
+	const failureMessage = document.getElementById("failure");
+
+	var response = await fetch(path, {
+		method: "POST",
+		headers: {
+			"Content-Type": "application/json",
+		},
+		body: JSON.stringify(body),
+	});
+
+	const json = await response.json();
+	if (json.token) {
+		window.localStorage.setItem("token", `"${json.token}"`);
+		window.location.href = "/app";
+		return;
+	}
+
+	if (json.ticket) {
+		// my terrible solution to 2fa
+		const twoFactorForm = document.forms["2fa"];
+		const loginForm = document.forms["login"];
+
+		twoFactorForm.style.display = "flex";
+		loginForm.style.display = "none";
+
+		twoFactorForm.ticket.value = json.ticket;
+		return;
+	}
+
+	// Very fun error message here lol
+	const error =
+		json.errors
+			? Object.values(json.errors)[0]._errors[0].message
+			: (
+				json.captcha_key ? "Captcha required" : json.message
+			);
+
+	failureMessage.innerHTML = error;
+	failureMessage.style.display = "block";
+};
\ No newline at end of file