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 53 additions and 49 deletions
Showing only changes of commit eec783bd77 - Show all commits

View file

@ -17,7 +17,7 @@
<p> <p>
Request a password reset sent to your linked email. <br> Enter either email address or skynet username. </br> Request a password reset sent to your linked email. <br> Enter either email address or skynet username. </br>
</p> </p>
<form id="recovery"> <form id="form">
<table id="table"> <table id="table">
<tr> <tr>
<td><label for="username">Username</label></td> <td><label for="username">Username</label></td>
@ -31,58 +31,12 @@
<td><input id="email" name="email" type="text"/> <br/></td> <td><input id="email" name="email" type="text"/> <br/></td>
</tr> </tr>
<tr> <tr>
<td colspan="2"><input type="submit" value="submit"/></td> <td colspan="2"><input id="button" type="submit" value="submit"/></td>
</tr> </tr>
</table> </table>
</form> </form>
<p id="formStatus"></p> <p id="formStatus"></p>
<script> <script src="password.js"></script>
const formEl = document.getElementById('recovery');
formEl.addEventListener('submit', formHandler);
async function formHandler(listener) {
listener.preventDefault();
// reset teh form status
document.getElementById('formStatus').innerHTML = "<span style='background-color: green; color: white'>Please wait.</span>";
const formData = new FormData(formEl);
const username = formData.get('username').trim();
const email = formData.get('email').trim();
if (username.length === 0 && email.length === 0) {
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Please enter username or email</span>";
return;
}
let to_send = {
email: email
};
// assuming username is not empty it is the preferred method
if (username.length > 0) {
to_send = {user: username};
}
let url = "https://api.account.skynet.ie/ldap/recover/password";
try {
let req = await fetch(url, {
method: 'POST',
body: JSON.stringify(to_send),
mode: "cors"
});
if (req.status === 200) {
document.getElementById('formStatus').innerHTML = "<span style='background-color: green; color: white'>Success Please check emails</span>";
} else {
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure: Failed to communicate to server</span>";
}
} catch (e) {
document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>Error: ${e}</span>`;
}
}
</script>
</main> </main>
<footer class="page-footer"> <footer class="page-footer">
UL Computer Society 2023-<span id="year">2023</span> UL Computer Society 2023-<span id="year">2023</span>

50
src/recovery/password.js Normal file
View file

@ -0,0 +1,50 @@
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 teh form status
form_status.innerHTML = "<span style='background-color: green; color: white'>Please wait.</span>";
const formData = new FormData(form);
const username = formData.get('username').trim();
const email = formData.get('email').trim();
if (username.length === 0 && email.length === 0) {
form_status.innerHTML = "<span style='background-color: red; color: white'>Please enter username or email</span>";
return;
}
let to_send = {
email: email
};
// assuming username is not empty it is the preferred method
if (username.length > 0) {
to_send = {user: username};
}
let url = "https://api.account.skynet.ie/ldap/recover/password";
try {
let req = await fetch(url, {
method: 'POST',
body: JSON.stringify(to_send),
mode: "cors"
});
if (req.status === 200) {
form_status.innerHTML = "<span style='background-color: green; color: white'>Success Please check emails</span>";
} else {
form_status.innerHTML = "<span style='background-color: red; color: white'>Failure: Failed to communicate to server</span>";
}
} catch (e) {
form_status.innerHTML = `<span style='background-color: red; color: white'>Error: ${e}</span>`;
}
}