aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/wwwroot/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'code/api/wwwroot/scripts')
-rw-r--r--code/api/wwwroot/scripts/base.js9
-rw-r--r--code/api/wwwroot/scripts/page-specific/login.js48
2 files changed, 57 insertions, 0 deletions
diff --git a/code/api/wwwroot/scripts/base.js b/code/api/wwwroot/scripts/base.js
new file mode 100644
index 0000000..3a55994
--- /dev/null
+++ b/code/api/wwwroot/scripts/base.js
@@ -0,0 +1,9 @@
+const session = {
+ _storageKey: "session_data",
+ get() {
+ return sessionStorage.getItem(session._storageKey);
+ },
+ set(data) {
+ sessionStorage.setItem(session._storageKey, JSON.stringify(data))
+ }
+} \ No newline at end of file
diff --git a/code/api/wwwroot/scripts/page-specific/login.js b/code/api/wwwroot/scripts/page-specific/login.js
new file mode 100644
index 0000000..ac43e5d
--- /dev/null
+++ b/code/api/wwwroot/scripts/page-specific/login.js
@@ -0,0 +1,48 @@
+const form = document.getElementById("login-form");
+
+
+function get_login_payload() {
+ return {
+ username: document.querySelector("input[name=username]").value,
+ password: document.querySelector("input[name=password]").value
+ }
+}
+
+function show_error(title, subtitle) {
+ if (!title || !subtitle) {
+ console.error("parameter title or subtitle is empty");
+ return;
+ }
+ const errorEl = form.querySelector(".error");
+ if (!errorEl) {
+ console.error("#" + form.id + " does not have an .error element")
+ return;
+ }
+ errorEl.querySelector(".title").innerText = title;
+ errorEl.querySelector(".subtitle").innerHTML = subtitle;
+}
+
+async function submit_login_form(event) {
+ event.preventDefault();
+ event.stopPropagation();
+ const response = await fetch("/account/login", {
+ method: "post",
+ body: JSON.stringify(get_login_payload()),
+ headers: {
+ "Content-Type": "application/json;charset=utf-8"
+ }
+ })
+
+ if (response.ok) {
+ const sessionResponse = await fetch("/session");
+ session.set(await sessionResponse.json());
+ location.href = "/home";
+ return;
+ }
+}
+
+function init() {
+ form.addEventListener("submit", submit_login_form)
+}
+
+document.addEventListener("DOMContentLoaded", init) \ No newline at end of file