fix login page error handling

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-04-19 21:24:34 +00:00
parent fcdfe020c6
commit aacc264314

View File

@ -1,4 +1,4 @@
import {requestTicket, setTicket, ResponseError, NetworkError, goToPage, deleteAllCookies, requestPVE} from "./utils.js";
import {requestTicket, setTicket, NetworkError, goToPage, deleteAllCookies, requestPVE} from "./utils.js";
import {alert} from "./dialog.js";
window.addEventListener("DOMContentLoaded", init);
@ -18,27 +18,26 @@ async function init (){
e.preventDefault();
let form = document.querySelector("form");
let formData = new FormData(form);
try {
formSubmitButton.innerText = "Authenticating...";
let ticket = await requestTicket(formData.get("username"), formData.get("password"), formData.get("realm"));
if (ticket.status === 200) {
setTicket(ticket.data.ticket, ticket.data.CSRFPreventionToken, formData.get("username"));
formSubmitButton.innerText = "LOGIN";
goToPage("index.html");
}
catch (error) {
if(error instanceof ResponseError) { // response error is usually 401 auth failed
else if (ticket.status === 401) {
alert("Authenticaton failed.");
formSubmitButton.innerText = "LOGIN";
}
else if (error instanceof NetworkError) {
else if (ticket.status === 408) {
alert("Network error.");
formSubmitButton.innerText = "LOGIN";
}
else {
alert("An error.");
alert("An error occured.");
formSubmitButton.innerText = "LOGIN";
console.error(error);
}
}
});
}