Fix errors not being propogated to teh end user. #23

Merged
silver merged 10 commits from #9-not-correctly-showing-errors into main 2024-01-01 10:11:02 +00:00
2 changed files with 58 additions and 50 deletions
Showing only changes of commit ae433303b8 - Show all commits

View file

@ -17,7 +17,7 @@
<p>
Enter a new password for your Skynet account below.
</p>
<form id="reset">
<form id="form">
<table id="table">
<tr>
<td><label for="pass1">New Password</label> </td>
@ -28,59 +28,12 @@
<td><input id="pass2" name="confirm" type="password"/> <br/> </td>
</tr>
<tr>
<td colspan="2"><input type="submit"/> </td>
<td colspan="2"><input id="button" type="submit"/> </td>
</tr>
</table>
</form>
<p id="formStatus"></p>
<script>
const formEl = document.getElementById("reset");
formEl.addEventListener('submit', (listener) => formHandler(listener));
const url = new URL(window.location.href);
const urlParam = new URLSearchParams(url.search);
const auth = urlParam.get("auth");
async function formHandler(listener) {
listener.preventDefault();
// reset
document.getElementById('formStatus').innerHTML = "<span style='background-color: green; color: white'>Please wait.</span>";
const formData = new FormData(formEl);
const pass = formData.get("password");
const confirm = formData.get("confirm");
if (pass !== confirm) {
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure: Passwords don't match</span>";
return;
}
let url = "https://api.account.skynet.ie/ldap/recover/password/auth";
let req;
try {
req = await fetch(url, {
method: 'POST',
body: JSON.stringify({auth: auth, pass: pass}),
mode: "cors"
});
} catch (e) {
document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>${e}</span>`;
return;
}
if (req.status !== 200) {
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure</span>";
return;
}
let data = await req.json();
if (data.result === 'error') {
document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>${data.error}</span>`;
} else {
document.getElementById('formStatus').innerHTML = "<span style='background-color: green; color: white'>Success</span>";
}
}
</script>
<script src="password_reset.js"></script>
</main>
<footer class="page-footer">
UL Computer Society 2023-<span id="year">2023</span>

View file

@ -0,0 +1,55 @@
const form = document.getElementById("form");
form.addEventListener('submit', formHandler);
const form_status = document.getElementById("formStatus");
const button = document.getElementById("button");
button.addEventListener('submit', formHandler);
async function formHandler(listener) {
listener.preventDefault();
// reset
form_status.innerHTML = "<span style='background-color: green; color: white'>Please wait.</span>";
const formData = new FormData(form);
const pass = formData.get("password");
const confirm = formData.get("confirm");
if (pass !== confirm) {
form_status.innerHTML = "<span style='background-color: red; color: white'>Failure: Passwords don't match</span>";
return;
}
const urlParam = new URLSearchParams(new URL(window.location.href).search);
const auth = urlParam.get("auth");
if(!auth){
return;
}
let url = "https://api.account.skynet.ie/ldap/recover/password/auth";
let req;
try {
req = await fetch(url, {
method: 'POST',
body: JSON.stringify({auth: auth, pass: pass}),
mode: "cors"
});
} catch (e) {
form_status.innerHTML = `<span style='background-color: red; color: white'>${e}</span>`;
return;
}
if (req.status !== 200) {
form_status.innerHTML = "<span style='background-color: red; color: white'>Failure</span>";
return;
}
let data = await req.json();
if (data.result === 'error') {
form_status.innerHTML = `<span style='background-color: red; color: white'>${data.error}</span>`;
} else {
form_status.innerHTML = "<span style='background-color: green; color: white'>Success</span>";
}
}