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
3 changed files with 112 additions and 97 deletions
Showing only changes of commit da7adea36e - Show all commits

View file

@ -1,97 +0,0 @@
<!DOCTYPE html>
<html lang="en-ie">
<head>
<meta charset="UTF-8"/>
<title>Skynet Register</title>
<link href="./images/favicon/favicon-16x16.png" rel="icon" type="image/png"/>
<link href="./stylesheets/index.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="wrapper">
<header class="page-header">
<img alt="Sharky, our mascot" height="81.56" src="./images/sharky.svg" width="145"/>
<h1>Skynet Self Service</h1>
</header>
<main class="page-body">
<h2>Register</h2>
<p>
Thank you for signing up for your Skynet account.
<br/>
Please complete the process by filling in the form below.
<br />
Username is what you want to be known on Skynet as.
<br>
It must be lowercase, start with a letter and contain only 0-9 a-z and _-
</p>
<form id="register">
<table id="table">
<tr>
<td><label for="user">Username</label></td>
<td><input id="user" name="user" type="text"/> <br/></td>
</tr>
<tr>
<td><label for="pass">Password</label></td>
<td><input id="pass" maxlength="254" minlength="8" name="pass" type="password"/> <br/></td>
</tr>
<tr>
<td><label for="confirm">Confirm</label></td>
<td><input id="confirm" name="confirm" type="password"/></td>
</tr>
<tr>
<td colspan="2"><input type="Submit" value="Submit"/></td>
</tr>
</table>
</form>
<p id="formStatus"></p>
<script>
const formEl = document.getElementById("register");
formEl.addEventListener('submit', (listener) => formHandler(listener));
function formHandler(listener) {
listener.preventDefault();
const formData = new FormData(formEl);
const pass = formData.get("pass");
if (pass === formData.get("confirm")) {
const url = new URL(window.location.href);
const urlParam = new URLSearchParams(url.search);
const auth = urlParam.get("auth");
const user = formData.get("user");
const object = {auth: auth, user: user, pass: pass};
fetch('https://api.account.skynet.ie/ldap/new/account', {
method: 'POST',
body: JSON.stringify(object),
mode: "cors"
}).then(res => {
if (res.status === 200) {
return res.json();
} else if (res.status === 500) {
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure</span>";
} else {
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure: Failed to communicate to server</span>";
}
})
.then((temp) => {
if (temp) {
if (temp.result === 'error') {
document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>${temp.error}</span>`;
} else {
document.getElementById('formStatus').innerHTML = "<span style='background-color: green; color: white'>Success</span>";
}
}
})
.catch(() => document.getElementById('formStatus').innerHTML = "<span style='background-color: yellow; color: black'>Please try again</span>");
} else {
document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>Passwords don't match</span>`;
}
}
</script>
</main>
<footer class="page-footer">
UL Computer Society 2023-<span id="year">2023</span>
<script>
document.getElementById('year').textContent = new Date().getFullYear().toString()
</script>
</footer>
</div>
</body>
</html>

56
src/register/index.html Normal file
View file

@ -0,0 +1,56 @@
<!DOCTYPE html>
<html lang="en-ie">
<head>
<meta charset="UTF-8"/>
<title>Skynet Register</title>
<link href="../images/favicon/favicon-16x16.png" rel="icon" type="image/png"/>
<link href="../stylesheets/index.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="wrapper">
<header class="page-header">
<img alt="Sharky, our mascot" height="81.56" src="../images/sharky.svg" width="145"/>
<h1>Skynet Self Service</h1>
</header>
<main class="page-body">
<h2>Register</h2>
<p>
Thank you for signing up for your Skynet account.
<br/>
Please complete the process by filling in the form below.
<br />
Username is what you want to be known on Skynet as.
<br>
It must be lowercase, start with a letter and contain only 0-9 a-z and _-
</p>
<form id="form">
<table id="table">
<tr>
<td><label for="user">Username</label></td>
<td><input id="user" name="user" type="text"/> <br/></td>
</tr>
<tr>
<td><label for="pass">Password</label></td>
<td><input id="pass" maxlength="254" minlength="8" name="pass" type="password"/> <br/></td>
</tr>
<tr>
<td><label for="confirm">Confirm</label></td>
<td><input id="confirm" name="confirm" type="password"/></td>
</tr>
<tr>
<td colspan="2"><input id="button" type="Submit" value="Submit"/></td>
</tr>
</table>
</form>
<p id="formStatus"></p>
<script src="register.js"></script>
</main>
<footer class="page-footer">
UL Computer Society 2023-<span id="year">2023</span>
<script>
document.getElementById('year').textContent = new Date().getFullYear().toString()
</script>
</footer>
</div>
</body>
</html>

56
src/register/register.js Normal file
View file

@ -0,0 +1,56 @@
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();
const formData = new FormData(form);
const pass = formData.get("pass");
if (pass !== formData.get("confirm")) {
form_status.innerHTML = `<span style='background-color: red; color: white'>Passwords don't match</span>`;
return;
}
const urlParam = new URLSearchParams(new URL(window.location.href).search);
const auth = urlParam.get("auth");
// if there is no auth value then return here
if(!auth){
return;
}
const user = formData.get("user");
const body = {auth: auth, user: user, pass: pass};
let url = 'https://api.account.skynet.ie/ldap/new/account';
let req;
try {
req = await fetch(url, {
method: 'POST',
body: JSON.stringify(body),
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 temp = await req.json();
if (temp.result === 'error') {
form_status.innerHTML = `<span style='background-color: red; color: white'>${temp.error}</span>`;
} else {
form_status.innerHTML = "<span style='background-color: green; color: white'>Success</span>";
}
}