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 === '') { // reset teh form status
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure: Please select an option, then try again</span>"; document.getElementById('formStatus').innerHTML = "";
} else {
const formData = new FormData(formEl); const formData = new FormData(formEl);
const value = formData.get('value'); const username = formData.get('username').trim();
let object; const email = formData.get('email').trim();
if (option === 'email') {
object = {email: value}; if(username.length === 0 && email.length === 0){
} else { document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Please enter username or email</span>";
object = {user: value}; return;
} }
fetch('https://api.account.skynet.ie/ldap/recover/password', {
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', method: 'POST',
body: JSON.stringify(object), body: JSON.stringify(to_send),
mode: "cors" mode: "cors"
}).then(res => { });
if (res.status === 200) {
document.getElementById('formStatus').innerHTML = "<span style='background-color: green; color: white'>Success</span>"; if (req.status === 200) {
} else if (res.status === 500) { document.getElementById('formStatus').innerHTML = "<span style='background-color: green; color: white'>Success Please check emails</span>";
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure</span>";
} else { } else {
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure: Failed to communicate to server</span>"; document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure: Failed to communicate to server</span>";
} }
}).catch(() => { } catch (e) {
document.getElementById('formStatus').innerHTML = "<span style='background-color: yellow; color: black'>Please try again</span>"; document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>Error: ${e}</span>`;
});
}
}
function selectField() {
const field = document.getElementById('options').value;
const inputEl = document.getElementById('value');
if (field === "email") {
inputEl.type = 'email';
} else {
inputEl.type = 'text';
} }
} }
</script> </script>