Gate the dashboard on the site's admin login and add a setup page
Like job-tracker, the tracker forwards the visitor's cookies to the site backend's /auth/validate-admin instead of asking for an API key. Signed in, the dashboard shows every device's data. The new /setup page has instructions for each collector and creates and revokes API keys. The userscript is served publicly at /tracker.user.js. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
+7
-37
@@ -26,27 +26,21 @@
|
||||
td.label { overflow-wrap:anywhere; }
|
||||
.bar { height:4px; background:var(--bar); border-radius:2px; margin-top:3px; }
|
||||
.ok { color:var(--ok); } .bad { color:var(--bad); }
|
||||
#login { margin-top:40px; display:flex; gap:8px; }
|
||||
#login input { flex:1; }
|
||||
a { color:var(--bar); }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<header>
|
||||
<h1>Activity Tracker</h1>
|
||||
<div class="nav" id="nav" hidden>
|
||||
<div class="nav">
|
||||
<button id="prev" aria-label="Previous day">‹</button>
|
||||
<input type="date" id="date">
|
||||
<button id="next" aria-label="Next day">›</button>
|
||||
<button id="logout" title="Forget API key">Sign out</button>
|
||||
<a href="setup">Setup</a>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<form id="login" hidden>
|
||||
<input type="password" id="key" placeholder="API key (trk_…)" autocomplete="off" required>
|
||||
<button>Open</button>
|
||||
</form>
|
||||
|
||||
<p id="status" class="muted"></p>
|
||||
<div id="content" hidden>
|
||||
<h2>Goals</h2><table id="goals"></table>
|
||||
@@ -57,12 +51,6 @@
|
||||
</main>
|
||||
<script>
|
||||
const $ = id => document.getElementById(id);
|
||||
const KEY = "tracker_api_key";
|
||||
const store = {
|
||||
get() { try { return localStorage.getItem(KEY) || ""; } catch { return ""; } },
|
||||
set(v) { try { v ? localStorage.setItem(KEY, v) : localStorage.removeItem(KEY); } catch {} },
|
||||
};
|
||||
let apiKey = store.get();
|
||||
|
||||
function dur(s) {
|
||||
s = Math.round(s);
|
||||
@@ -75,9 +63,10 @@ const esc = s => String(s ?? "").replace(/[&<>"]/g, c => ({ "&":"&", "<":"&l
|
||||
const today = () => new Date().toLocaleDateString("en-CA"); // YYYY-MM-DD in local time
|
||||
|
||||
async function api(path) {
|
||||
const r = await fetch("api/v1/" + path, { headers: { Authorization: "Bearer " + apiKey } });
|
||||
// Authenticated by the main site's session cookies, which the browser sends.
|
||||
const r = await fetch("api/v1/" + path);
|
||||
const body = await r.json().catch(() => ({}));
|
||||
if (r.status === 401) { signOut(); throw new Error(body.error || "unauthorized"); }
|
||||
if (r.status === 401) { location.reload(); throw new Error("session expired"); } // reload goes to the login page
|
||||
if (!r.ok) throw new Error(body.error || r.statusText);
|
||||
return body;
|
||||
}
|
||||
@@ -135,30 +124,11 @@ function shift(days) {
|
||||
load();
|
||||
}
|
||||
|
||||
function signOut() {
|
||||
apiKey = ""; store.set("");
|
||||
show();
|
||||
}
|
||||
|
||||
function show() {
|
||||
$("login").hidden = !!apiKey;
|
||||
$("nav").hidden = !apiKey;
|
||||
$("content").hidden = true;
|
||||
$("status").textContent = "";
|
||||
if (apiKey) load();
|
||||
}
|
||||
|
||||
$("login").onsubmit = e => {
|
||||
e.preventDefault();
|
||||
apiKey = $("key").value.trim(); store.set(apiKey); $("key").value = "";
|
||||
show();
|
||||
};
|
||||
$("logout").onclick = signOut;
|
||||
$("prev").onclick = () => shift(-1);
|
||||
$("next").onclick = () => shift(1);
|
||||
$("date").onchange = load;
|
||||
$("date").value = today();
|
||||
show();
|
||||
load();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user