From b208088c0e32dd9eb83c724616e82fb12a63c445 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Mon, 18 Jul 2022 17:27:25 +1000 Subject: [PATCH] Display network/api errors on login/register page --- slowcord/login/package-lock.json | Bin 358503 -> 358505 bytes slowcord/login/public/js/handler.js | 23 +++++++++++++++++++++++ slowcord/login/public/login.html | 24 +++++------------------- slowcord/login/public/register.html | 28 +++++++--------------------- 4 files changed, 35 insertions(+), 40 deletions(-) create mode 100644 slowcord/login/public/js/handler.js diff --git a/slowcord/login/package-lock.json b/slowcord/login/package-lock.json index 60f36fa5ba6ed266fe344eee638dfce273f38e1c..05d385a3d05dff7a5b18d031e02907431d9c4903 100644 GIT binary patch delta 65 zcmaF9c8F5a(o65}H RKEa(Ch*`EzaA&=4006t36;=QM delta 69 zcmaF)LG<|t(G8L;Y{q)#N;+KA1&^`_PuF#4wrwt8X)j=51Y)M`1uV>qosd { + 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; + } + + const error = json.errors ? Object.values(json.errors)[0]._errors[0].message : json.message; + + failureMessage.innerHTML = error; + failureMessage.style.display = "block"; +} \ No newline at end of file diff --git a/slowcord/login/public/login.html b/slowcord/login/public/login.html index ef2d91c1..bbfbf2af 100644 --- a/slowcord/login/public/login.html +++ b/slowcord/login/public/login.html @@ -11,6 +11,7 @@ + @@ -67,25 +68,10 @@ const email = data.get("email"); const password = data.get("password"); - const response = await fetch("/api/v9/auth/login", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - login: email, - password: password, - }) - }); - - const json = await response.json(); - if (json.token) { - window.localStorage.setItem("token", `"${json.token}"`); - window.location.href = "/app"; - return; - } - - document.getElementById("failure").style.display = "block"; + await handleSubmit("/api/v9/auth/login", { + login: email, + password: password, + }) }) diff --git a/slowcord/login/public/register.html b/slowcord/login/public/register.html index 5843649c..9e7457a1 100644 --- a/slowcord/login/public/register.html +++ b/slowcord/login/public/register.html @@ -11,6 +11,7 @@ + @@ -58,28 +59,13 @@ const password = data.get("password"); const dob = data.get("dob"); - const response = await fetch("/api/v9/auth/register", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - consent: true, - email: email, - username: username, - password: password, - date_of_birth: dob, - }) + await handleSubmit("/api/v9/auth/register", { + consent: true, + email: email, + username: username, + password: password, + date_of_birth: dob, }); - - const json = await response.json(); - if (json.token) { - window.localStorage.setItem("token", `"${json.token}"`); - window.location.href = "/app"; - return; - } - - document.getElementById("failure").style.display = "block"; })