2023-04-19 21:24:34 +00:00
|
|
|
import {requestTicket, setTicket, NetworkError, goToPage, deleteAllCookies, requestPVE} from "./utils.js";
|
2023-04-18 20:08:59 +00:00
|
|
|
import {alert} from "./dialog.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");
|
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) => {
|
|
|
|
e.preventDefault();
|
|
|
|
let form = document.querySelector("form");
|
|
|
|
let formData = new FormData(form);
|
2023-04-19 21:24:34 +00:00
|
|
|
|
|
|
|
formSubmitButton.innerText = "Authenticating...";
|
|
|
|
let ticket = await requestTicket(formData.get("username"), formData.get("password"), formData.get("realm"));
|
|
|
|
if (ticket.status === 200) {
|
2023-02-14 05:21:45 +00:00
|
|
|
setTicket(ticket.data.ticket, ticket.data.CSRFPreventionToken, formData.get("username"));
|
2023-04-18 20:08:59 +00:00
|
|
|
formSubmitButton.innerText = "LOGIN";
|
2022-12-19 05:58:46 +00:00
|
|
|
goToPage("index.html");
|
2022-12-12 23:11:52 +00:00
|
|
|
}
|
2023-04-19 21:24:34 +00:00
|
|
|
else if (ticket.status === 401) {
|
|
|
|
alert("Authenticaton failed.");
|
|
|
|
formSubmitButton.innerText = "LOGIN";
|
|
|
|
}
|
|
|
|
else if (ticket.status === 408) {
|
|
|
|
alert("Network error.");
|
|
|
|
formSubmitButton.innerText = "LOGIN";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alert("An error occured.");
|
|
|
|
formSubmitButton.innerText = "LOGIN";
|
|
|
|
console.error(error);
|
2022-12-12 23:11:52 +00:00
|
|
|
}
|
|
|
|
});
|
2022-12-12 22:57:43 +00:00
|
|
|
}
|