115 lines
3.2 KiB
JavaScript
115 lines
3.2 KiB
JavaScript
const csrfMeta = document.getElementById("csrf-meta");
|
|
const refresh = document.getElementById("refresh");
|
|
const status = document.getElementById("account-status");
|
|
const form = document.getElementById("account-form");
|
|
const facts = document.getElementById("account-facts");
|
|
|
|
let sessionCSRF = csrfMeta ? csrfMeta.content : "";
|
|
|
|
async function loadSession() {
|
|
const response = await fetch("/api/session", { credentials: "same-origin" });
|
|
if (response.status === 401) {
|
|
window.location.href = "/login";
|
|
return null;
|
|
}
|
|
const session = await response.json();
|
|
if (!session.authenticated) {
|
|
window.location.href = "/login";
|
|
return null;
|
|
}
|
|
if (session.csrf_token) {
|
|
sessionCSRF = session.csrf_token;
|
|
}
|
|
renderAccount(session);
|
|
return session;
|
|
}
|
|
|
|
function renderAccount(session) {
|
|
facts.innerHTML = `
|
|
<div><dt>Username</dt><dd>${escapeHTML(session.username || "Unknown")}</dd></div>
|
|
<div><dt>Role</dt><dd>${escapeHTML(session.role || "user")}</dd></div>
|
|
<div><dt>Printer grants</dt><dd>${renderGrants(session.printer_grants || [])}</dd></div>
|
|
`;
|
|
}
|
|
|
|
function renderGrants(grants) {
|
|
if (!grants.length) {
|
|
return "No printer access assigned.";
|
|
}
|
|
return grants.map((grant) => `${escapeHTML(grant.printer_id)} (${escapeHTML(grant.access || "read")})`).join("<br>");
|
|
}
|
|
|
|
async function apiRequest(url, method, body) {
|
|
const options = {
|
|
method,
|
|
credentials: "same-origin",
|
|
headers: {
|
|
"X-CSRF-Token": sessionCSRF,
|
|
},
|
|
};
|
|
if (body) {
|
|
options.headers["Content-Type"] = "application/json";
|
|
options.body = JSON.stringify(body);
|
|
}
|
|
const response = await fetch(url, options);
|
|
if (!response.ok) {
|
|
const text = await response.text();
|
|
throw new Error(text || `Request failed: ${response.status}`);
|
|
}
|
|
return response;
|
|
}
|
|
|
|
function setStatus(message, isError = false) {
|
|
if (!message) {
|
|
status.hidden = true;
|
|
status.textContent = "";
|
|
status.classList.remove("error", "success");
|
|
return;
|
|
}
|
|
status.hidden = false;
|
|
status.textContent = message;
|
|
status.classList.toggle("error", isError);
|
|
status.classList.toggle("success", !isError);
|
|
}
|
|
|
|
form.addEventListener("submit", async (event) => {
|
|
event.preventDefault();
|
|
setStatus("");
|
|
const data = new FormData(form);
|
|
const currentPassword = String(data.get("current_password") || "");
|
|
const newPassword = String(data.get("new_password") || "");
|
|
const confirmPassword = String(data.get("confirm_password") || "");
|
|
if (newPassword !== confirmPassword) {
|
|
setStatus("New passwords do not match.", true);
|
|
return;
|
|
}
|
|
try {
|
|
await apiRequest("/api/account", "PATCH", {
|
|
current_password: currentPassword,
|
|
new_password: newPassword,
|
|
});
|
|
form.reset();
|
|
setStatus("Password updated.", false);
|
|
} catch (error) {
|
|
setStatus(error instanceof Error ? error.message : "Unable to update password.", true);
|
|
}
|
|
});
|
|
|
|
refresh.addEventListener("click", async () => {
|
|
setStatus("");
|
|
await loadSession();
|
|
});
|
|
|
|
function escapeHTML(value) {
|
|
return String(value)
|
|
.replaceAll("&", "&")
|
|
.replaceAll("<", "<")
|
|
.replaceAll(">", ">")
|
|
.replaceAll('"', """)
|
|
.replaceAll("'", "'");
|
|
}
|
|
|
|
(async () => {
|
|
await loadSession();
|
|
})();
|