feat(server): admin panel pages for dashboard, users, archive, jobs, settings and logs
Vanilla ES modules under a strict CSP (bars and charts are SVG, no inline styles), cookie session with CSRF header, first sign-in password change, live updates over the events WebSocket, language menu fed by /api/v1/i18n. Every label and tooltip is an Admin.* resource (en, it). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
16 files changed
+2029
-106
No files matched your search
@@ -1,13 +1,16 @@
|
||||
// Same API as the desktop clients: cookie session + double-submit CSRF token for the web admin (BRIEF §14.2).
|
||||
// Same API as the desktop clients: cookie session + CSRF header for the web admin (BRIEF §14.2).
|
||||
import { culture } from "./i18n.js";
|
||||
|
||||
export class ApiFailure extends Error {
|
||||
constructor(status, code, message) {
|
||||
constructor(status, code, message, details) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
this.code = code;
|
||||
this.details = details;
|
||||
}
|
||||
}
|
||||
|
||||
const SESSION_KEY = "titano.admin.user";
|
||||
let currentUser = null;
|
||||
|
||||
function csrfToken() {
|
||||
const m = document.cookie.match(/(?:^|; )titano_csrf=([^;]+)/);
|
||||
@@ -15,15 +18,27 @@ function csrfToken() {
|
||||
}
|
||||
|
||||
async function request(method, url, body) {
|
||||
const headers = { Accept: "application/json" };
|
||||
const headers = { Accept: "application/json", "Accept-Language": culture() };
|
||||
if (body !== undefined) headers["Content-Type"] = "application/json";
|
||||
if (method !== "GET") headers["X-CSRF-Token"] = csrfToken();
|
||||
const res = await fetch(url, { method, headers, credentials: "same-origin", body: body === undefined ? undefined : JSON.stringify(body) });
|
||||
if (res.status === 401 && url !== "/api/v1/auth/login") sessionStorage.removeItem(SESSION_KEY);
|
||||
let res;
|
||||
try {
|
||||
res = await fetch(url, { method, headers, credentials: "same-origin", body: body === undefined ? undefined : JSON.stringify(body) });
|
||||
} catch {
|
||||
throw new ApiFailure(0, "unreachable", "");
|
||||
}
|
||||
if (res.status === 401 && url !== "/api/v1/auth/login") {
|
||||
currentUser = null;
|
||||
window.dispatchEvent(new Event("titano:signed-out"));
|
||||
}
|
||||
if (!res.ok) {
|
||||
let err = { code: "http_" + res.status, message: res.statusText };
|
||||
try { err = await res.json(); } catch { /* non-JSON error */ }
|
||||
throw new ApiFailure(res.status, err.code, err.message);
|
||||
try {
|
||||
err = await res.json();
|
||||
} catch {
|
||||
// Not a JSON error body.
|
||||
}
|
||||
throw new ApiFailure(res.status, err.code, err.message, err.details);
|
||||
}
|
||||
return res.status === 204 ? null : res.json();
|
||||
}
|
||||
@@ -31,17 +46,71 @@ async function request(method, url, body) {
|
||||
export const api = {
|
||||
get: (url) => request("GET", url),
|
||||
post: (url, body) => request("POST", url, body ?? {}),
|
||||
put: (url, body) => request("PUT", url, body),
|
||||
patch: (url, body) => request("PATCH", url, body),
|
||||
del: (url) => request("DELETE", url),
|
||||
isSignedIn: () => sessionStorage.getItem(SESSION_KEY) !== null,
|
||||
user: () => JSON.parse(sessionStorage.getItem(SESSION_KEY) ?? "null"),
|
||||
user: () => currentUser,
|
||||
|
||||
/** Restores the session from the cookie; null when signed out. */
|
||||
async resume() {
|
||||
try {
|
||||
currentUser = await request("GET", "/api/v1/auth/me");
|
||||
} catch {
|
||||
currentUser = null;
|
||||
}
|
||||
return currentUser;
|
||||
},
|
||||
|
||||
async signIn(username, password) {
|
||||
const r = await request("POST", "/api/v1/auth/login", { username, password, clientName: "admin-web", cookie: true });
|
||||
sessionStorage.setItem(SESSION_KEY, JSON.stringify(r.user));
|
||||
currentUser = r.user;
|
||||
return r;
|
||||
},
|
||||
|
||||
async changePassword(currentPassword, newPassword) {
|
||||
await request("POST", "/api/v1/auth/password", { currentPassword, newPassword });
|
||||
currentUser = await request("GET", "/api/v1/auth/me");
|
||||
},
|
||||
|
||||
async signOut() {
|
||||
try { await request("POST", "/api/v1/auth/logout", {}); } catch { /* already gone */ }
|
||||
sessionStorage.removeItem(SESSION_KEY);
|
||||
try {
|
||||
await request("POST", "/api/v1/auth/logout", {});
|
||||
} catch {
|
||||
// Already signed out.
|
||||
}
|
||||
currentUser = null;
|
||||
},
|
||||
|
||||
/** Server events over WebSocket (cookie session), reconnecting with back-off until stop() is called. */
|
||||
events(onEvent) {
|
||||
let socket = null;
|
||||
let stopped = false;
|
||||
let delay = 1000;
|
||||
const connect = () => {
|
||||
if (stopped) return;
|
||||
socket = new WebSocket(`${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/api/v1/events`);
|
||||
socket.onopen = () => {
|
||||
delay = 1000;
|
||||
};
|
||||
socket.onmessage = (m) => {
|
||||
try {
|
||||
onEvent(JSON.parse(m.data));
|
||||
} catch {
|
||||
// Ignore malformed or unknown events.
|
||||
}
|
||||
};
|
||||
socket.onclose = () => {
|
||||
if (stopped) return;
|
||||
setTimeout(connect, delay);
|
||||
delay = Math.min(delay * 2, 30000);
|
||||
};
|
||||
};
|
||||
connect();
|
||||
return {
|
||||
stop() {
|
||||
stopped = true;
|
||||
socket?.close();
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user