From 1184d47a788ae3320f90de241bf85cc4fc546f97 Mon Sep 17 00:00:00 2001
From: LarsVomMars
Date: Sat, 30 Jan 2021 17:15:53 +0100
Subject: Auth QOL changes

---
 auth/public/change.html |  2 +-
 auth/public/index.html  |  1 +
 auth/public/script.js   | 22 +++++++++++++++++++++-
 3 files changed, 23 insertions(+), 2 deletions(-)

(limited to 'auth/public')

diff --git a/auth/public/change.html b/auth/public/change.html
index f703649..5652af4 100644
--- a/auth/public/change.html
+++ b/auth/public/change.html
@@ -41,7 +41,7 @@
                     minlength="8"
                     required
                 />
-                <button type="submit" class="pure-button pure-button-primary">Anmelden</button>
+                <button type="submit" class="pure-button pure-button-primary">Passwort ändern</button>
             </fieldset>
         </form>
 
diff --git a/auth/public/index.html b/auth/public/index.html
index 8273238..e0980d1 100644
--- a/auth/public/index.html
+++ b/auth/public/index.html
@@ -32,5 +32,6 @@
                 <button type="submit" class="pure-button pure-button-primary">Anmelden</button>
             </fieldset>
         </form>
+        <script src="script.js"></script>
     </body>
 </html>
diff --git a/auth/public/script.js b/auth/public/script.js
index b50bf9b..fd1fb3a 100644
--- a/auth/public/script.js
+++ b/auth/public/script.js
@@ -2,5 +2,25 @@ loggedIn();
 
 async function loggedIn() {
     const resp = await fetch("api/status");
-    if (!(await resp.json())["loggedIn"]) location.redirect("/");
+    const res = await resp.json();
+    if (res.loggedIn && !window.location.pathname.endsWith("change.html")) window.location.replace("/");
+    else if (!res.loggedIn && window.location.pathname.endsWith("change.html")) window.location.replace("/");
 }
+
+const form = document.querySelector("form");
+form.addEventListener("submit", async e => {
+    e.preventDefault();
+    const method = e.target.method;
+    const url = e.target.action;
+    const rawBody = {};
+    for (const input of form.querySelectorAll("input"))
+        rawBody[input.name] = input.value;
+    const body = JSON.stringify(rawBody);
+    const resp = await fetch(url, { method, body, headers: { "Content-Type": "application/json" } });
+    const res = await resp.json();
+    if (!res.success) alert(res.message);
+    else {
+        const ref = new URL(location.href).searchParams.get("ref");
+        window.location.replace(ref);
+    }
+});
\ No newline at end of file
-- 
cgit v1.2.3