2023-03-24 21:13:33 +00:00
|
|
|
import {requestTicket, setTicket, ResponseError, NetworkError, goToPage, deleteAllCookies, requestPVE} from "./utils.js";
|
2022-12-12 22:57:43 +00:00
|
|
|
|
|
|
|
window.addEventListener("DOMContentLoaded", init);
|
|
|
|
|
2023-03-24 21:13:33 +00:00
|
|
|
async function init (){
|
2023-01-09 23:08:45 +00:00
|
|
|
deleteAllCookies();
|
2022-12-12 22:57:43 +00:00
|
|
|
let formSubmitButton = document.querySelector("#submit");
|
2022-12-13 01:12:11 +00:00
|
|
|
let status = document.querySelector("#status");
|
2023-03-24 21:13:33 +00:00
|
|
|
let realms = await requestPVE("/access/domains", "GET");
|
|
|
|
let realmSelect = document.querySelector("#realm");
|
|
|
|
realms.data.forEach((element) => {
|
|
|
|
realmSelect.add(new Option(element.comment, element.realm));
|
2023-03-24 22:25:14 +00:00
|
|
|
if("default" in element && element.default === 1){
|
|
|
|
realmSelect.value = element.realm;
|
|
|
|
}
|
2023-03-24 21:13:33 +00:00
|
|
|
});
|
2022-12-12 23:11:52 +00:00
|
|
|
formSubmitButton.addEventListener("click", async (e) => {
|
2022-12-13 01:12:11 +00:00
|
|
|
status.innerText = "";
|
2023-04-03 22:59:36 +00:00
|
|
|
status.style.color = "var(--content-txt-color)";
|
2022-12-12 23:11:52 +00:00
|
|
|
e.preventDefault();
|
|
|
|
let form = document.querySelector("form");
|
|
|
|
let formData = new FormData(form);
|
|
|
|
try {
|
2022-12-13 01:12:11 +00:00
|
|
|
status.innerText = "Authenticating...";
|
2023-03-24 21:13:33 +00:00
|
|
|
let ticket = await requestTicket(formData.get("username"), formData.get("password"), formData.get("realm"));
|
2023-02-14 05:21:45 +00:00
|
|
|
setTicket(ticket.data.ticket, ticket.data.CSRFPreventionToken, formData.get("username"));
|
2023-01-09 23:20:34 +00:00
|
|
|
status.innerText = "Authentication successful!"
|
2023-04-03 22:59:36 +00:00
|
|
|
status.style.color = "var(--success-color)";
|
2022-12-19 05:58:46 +00:00
|
|
|
goToPage("index.html");
|
2022-12-12 23:11:52 +00:00
|
|
|
}
|
|
|
|
catch (error) {
|
2022-12-13 01:12:11 +00:00
|
|
|
if(error instanceof ResponseError) { // response error is usually 401 auth failed
|
|
|
|
status.innerText = "Authentication failed.";
|
2023-04-03 22:59:36 +00:00
|
|
|
status.style.color = "var(--fail-color)";
|
2022-12-13 01:12:11 +00:00
|
|
|
}
|
|
|
|
else if (error instanceof NetworkError) {
|
|
|
|
status.innerText = "Encountered a network error.";
|
2023-04-03 22:59:36 +00:00
|
|
|
status.style.color = "var(--fail-color)";
|
2022-12-13 01:12:11 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
status.innerText = "An error occured.";
|
2023-04-03 22:59:36 +00:00
|
|
|
status.style.color = "var(--fail-color)";
|
2022-12-13 01:12:11 +00:00
|
|
|
console.error(error);
|
|
|
|
}
|
2022-12-12 23:11:52 +00:00
|
|
|
}
|
|
|
|
});
|
2022-12-12 22:57:43 +00:00
|
|
|
}
|