feat: Moved the JS out of the password recovery page into its own file

Related to #9
This commit is contained in:
silver 2024-01-01 09:31:32 +00:00
parent ad55bf81ef
commit eec783bd77
2 changed files with 53 additions and 49 deletions

View file

@ -17,7 +17,7 @@
<p>
Request a password reset sent to your linked email. <br> Enter either email address or skynet username. </br>
</p>
<form id="recovery">
<form id="form">
<table id="table">
<tr>
<td><label for="username">Username</label></td>
@ -31,58 +31,12 @@
<td><input id="email" name="email" type="text"/> <br/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="submit"/></td>
<td colspan="2"><input id="button" type="submit" value="submit"/></td>
</tr>
</table>
</form>
<p id="formStatus"></p>
<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>
<script src="password.js"></script>
</main>
<footer class="page-footer">
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>`;
}
}