2022-12-19 06:00:59 +00:00
|
|
|
import {requestTicket, setTicket, ResponseError, NetworkError, goToPage} from "./utils.js";
|
2022-12-12 22:57:43 +00:00
|
|
|
|
|
|
|
window.addEventListener("DOMContentLoaded", init);
|
|
|
|
|
|
|
|
function init (){
|
|
|
|
let formSubmitButton = document.querySelector("#submit");
|
2022-12-13 01:12:11 +00:00
|
|
|
let status = document.querySelector("#status");
|
2022-12-12 23:11:52 +00:00
|
|
|
formSubmitButton.addEventListener("click", async (e) => {
|
2022-12-13 01:12:11 +00:00
|
|
|
status.innerText = "";
|
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...";
|
2022-12-12 23:11:52 +00:00
|
|
|
let ticket = await requestTicket(formData.get("username"), formData.get("password"));
|
2022-12-16 07:15:52 +00:00
|
|
|
setTicket(ticket.data.ticket, ticket.data.CSRFPreventionToken);
|
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.";
|
|
|
|
status.style.color = "#ff0000";
|
|
|
|
}
|
|
|
|
else if (error instanceof NetworkError) {
|
|
|
|
status.innerText = "Encountered a network error.";
|
|
|
|
status.style.color = "#ff0000";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
status.innerText = "An error occured.";
|
|
|
|
status.style.color = "#ff0000";
|
|
|
|
console.error(error);
|
|
|
|
}
|
2022-12-12 23:11:52 +00:00
|
|
|
}
|
|
|
|
});
|
2022-12-12 22:57:43 +00:00
|
|
|
}
|