feat: password reset request form is functional

This commit is contained in:
silver 2023-09-26 18:16:38 +01:00
parent 7105bb2a9b
commit b2c0d939f9

View file

@ -7,14 +7,13 @@
</head> </head>
<body> <body>
<form id="recovery"> <form id="recovery">
<label for="options">Options</label> <label for="username">Username</label>
<select onChange="selectField()" id="options"> <input type="text" id="username" name="username"/>
<option value="" selected="selected">Please select an option</option> <p>Or</p>
<option value="user">Username</option> <label for="email">Email</label>
<option value="email">Email</option> <input type="text" id="email" name="email"/>
</select> <br/> <br/>
<label for="value">Value</label> <br/>
<input type="text" id="value" name="value"/> <br/>
<input type="submit" value="submit"/> <input type="submit" value="submit"/>
</form> </form>
<p id="formStatus"></p> <p id="formStatus"></p>
@ -23,47 +22,48 @@
</footer> </footer>
<script> <script>
const formEl = document.getElementById('recovery'); const formEl = document.getElementById('recovery');
formEl.addEventListener('submit', (listener) => formHandler(listener)); formEl.addEventListener('submit', formHandler);
function formHandler(listener) { async function formHandler(listener) {
listener.preventDefault(); listener.preventDefault();
const option = document.getElementById('options').value.toString();
if (option === '') {
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure: Please select an option, then try again</span>";
} else {
const formData = new FormData(formEl);
const value = formData.get('value');
let object;
if (option === 'email') {
object = {email: value};
} else {
object = {user: value};
}
fetch('https://api.account.skynet.ie/ldap/recover/password', {
method: 'POST',
body: JSON.stringify(object),
mode: "cors"
}).then(res => {
if (res.status === 200) {
document.getElementById('formStatus').innerHTML = "<span style='background-color: green; color: white'>Success</span>";
} 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>";
}
}).catch(() => {
document.getElementById('formStatus').innerHTML = "<span style='background-color: yellow; color: black'>Please try again</span>";
});
}
}
function selectField() { // reset teh form status
const field = document.getElementById('options').value; document.getElementById('formStatus').innerHTML = "";
const inputEl = document.getElementById('value');
if (field === "email") { const formData = new FormData(formEl);
inputEl.type = 'email'; const username = formData.get('username').trim();
} else { const email = formData.get('email').trim();
inputEl.type = 'text';
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>