improve login style and logic
Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
@@ -41,4 +41,23 @@ export class Dialog extends HTMLElement {
|
||||
}
|
||||
}
|
||||
|
||||
export function alert (message) {
|
||||
let form = document.createElement("form");
|
||||
form.method = "dialog";
|
||||
form.innerHTML = `
|
||||
<p class="w3-center" style="margin-bottom: 0px;">${message}</p>
|
||||
<div class="w3-center">
|
||||
<button class="w3-button w3-margin" id="submit">OK</button>
|
||||
</div>
|
||||
`;
|
||||
|
||||
let dialog = document.createElement("dialog");
|
||||
dialog.classList.add("w3-card-4");
|
||||
dialog.classList.add("w3-container");
|
||||
dialog.append(form);
|
||||
|
||||
document.body.append(dialog);
|
||||
dialog.showModal();
|
||||
}
|
||||
|
||||
customElements.define("dialog-form", Dialog);
|
@@ -1,11 +1,11 @@
|
||||
import {requestTicket, setTicket, ResponseError, NetworkError, goToPage, deleteAllCookies, requestPVE} from "./utils.js";
|
||||
import {alert} from "./dialog.js";
|
||||
|
||||
window.addEventListener("DOMContentLoaded", init);
|
||||
|
||||
async function init (){
|
||||
deleteAllCookies();
|
||||
let formSubmitButton = document.querySelector("#submit");
|
||||
let status = document.querySelector("#status");
|
||||
let realms = await requestPVE("/access/domains", "GET");
|
||||
let realmSelect = document.querySelector("#realm");
|
||||
realms.data.forEach((element) => {
|
||||
@@ -15,31 +15,28 @@ async function init (){
|
||||
}
|
||||
});
|
||||
formSubmitButton.addEventListener("click", async (e) => {
|
||||
status.innerText = "";
|
||||
status.style.color = "var(--content-txt-color)";
|
||||
e.preventDefault();
|
||||
let form = document.querySelector("form");
|
||||
let formData = new FormData(form);
|
||||
try {
|
||||
status.innerText = "Authenticating...";
|
||||
formSubmitButton.innerText = "Authenticating...";
|
||||
let ticket = await requestTicket(formData.get("username"), formData.get("password"), formData.get("realm"));
|
||||
setTicket(ticket.data.ticket, ticket.data.CSRFPreventionToken, formData.get("username"));
|
||||
status.innerText = "Authentication successful!"
|
||||
status.style.color = "var(--success-color)";
|
||||
formSubmitButton.innerText = "LOGIN";
|
||||
goToPage("index.html");
|
||||
}
|
||||
catch (error) {
|
||||
if(error instanceof ResponseError) { // response error is usually 401 auth failed
|
||||
status.innerText = "Authentication failed.";
|
||||
status.style.color = "var(--fail-color)";
|
||||
alert("Authenticaton failed.");
|
||||
formSubmitButton.innerText = "LOGIN";
|
||||
}
|
||||
else if (error instanceof NetworkError) {
|
||||
status.innerText = "Encountered a network error.";
|
||||
status.style.color = "var(--fail-color)";
|
||||
alert("Network error.");
|
||||
formSubmitButton.innerText = "LOGIN";
|
||||
}
|
||||
else {
|
||||
status.innerText = "An error occured.";
|
||||
status.style.color = "var(--fail-color)";
|
||||
alert("An error.");
|
||||
formSubmitButton.innerText = "LOGIN";
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user