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