Gate the admin panel behind a real login screen

/admin now shows only a token field + "Accedi" button on first load —
config and users are structurally in the page but empty/hidden, no data
requested until the token is verified. On successful login (a GET
/admin/config that doesn't 403) it reveals the dashboard and loads
config + users automatically; no more separate "Carica configurazione"/
"Carica utenti" buttons. Token is kept in sessionStorage (cleared on
tab close) so a reload during the same session skips straight back to
the dashboard. Added a Logout button and Enter-to-submit on the token
field.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
davideandClaude Sonnet 5 committed 2026-07-21 14:28:29 +02:00
1 parent f6035a888b
commit 48a9eeb839
1 file changed
+73 -31
+73 -31
View File
@@ -124,32 +124,42 @@
</head> </head>
<body> <body>
<header> <section id="login-section">
<header>
<h1>PLM Lottery — Admin</h1> <h1>PLM Lottery — Admin</h1>
<p>Configurazione operativa del round</p> <p>Accesso riservato</p>
</header> </header>
<div class="card"> <div class="card">
<label for="admin-token">Admin token</label>
<input id="admin-token" type="password" placeholder="valore di ADMIN_TOKEN" autofocus>
<button onclick="adminLogin()" id="login-btn">Accedi</button>
</div>
</section>
<section id="dashboard-section" class="hidden">
<header>
<div class="row-between" style="display:flex;align-items:center;justify-content:space-between">
<h1 style="margin:0">PLM Lottery — Admin</h1>
<button class="secondary" style="width:auto;margin-top:0;min-height:36px" onclick="adminLogout()">Esci</button>
</div>
<p>Configurazione operativa del round</p>
</header>
<div class="card">
<h2 style="font-size:1rem;font-weight:600;margin:0 0 4px">Configurazione</h2>
<p class="hint"> <p class="hint">
Salvata nel database, modificabile in qualsiasi momento senza riavviare il server. Salvata nel database, modificabile in qualsiasi momento senza riavviare il server.
Serve il token admin (<code>ADMIN_TOKEN</code> nel <code>.env</code> del server).
</p> </p>
<label for="admin-token">Admin token</label>
<input id="admin-token" type="password" placeholder="valore di ADMIN_TOKEN">
<button class="secondary" onclick="adminLoad()" id="load-btn">Carica configurazione attuale</button>
<div id="admin-form" class="hidden">
<label for="admin-fee-address">Fee address (dove finisce il 30% di ogni round)</label> <label for="admin-fee-address">Fee address (dove finisce il 30% di ogni round)</label>
<input id="admin-fee-address" class="mono" placeholder="plm1q..."> <input id="admin-fee-address" class="mono" placeholder="plm1q...">
<label for="admin-bet-amount">Bet amount (PLM)</label> <label for="admin-bet-amount">Bet amount (PLM)</label>
<input id="admin-bet-amount" inputmode="decimal" placeholder="es. 10"> <input id="admin-bet-amount" inputmode="decimal" placeholder="es. 10">
<button onclick="adminSave()" id="save-btn">Salva</button> <button onclick="adminSave()" id="save-btn">Salva</button>
</div> </div>
</div>
<div class="card"> <div class="card">
<h2 style="font-size:1rem;font-weight:600;margin:0 0 4px">Utenti</h2> <h2 style="font-size:1rem;font-weight:600;margin:0 0 4px">Utenti</h2>
<p class="hint">Elenco utenti registrati, con saldo interno e accesso alla chiave privata per interventi manuali (es. restituire fondi bloccati).</p> <p class="hint">Elenco utenti registrati, con saldo interno e accesso alla chiave privata per interventi manuali (es. restituire fondi bloccati).</p>
@@ -157,9 +167,7 @@
⚠ La chiave privata dà accesso completo ai fondi dell'utente. Ogni volta che la visualizzi viene registrata nell'audit log del server. Non condividerla, non salvarla altrove. ⚠ La chiave privata dà accesso completo ai fondi dell'utente. Ogni volta che la visualizzi viene registrata nell'audit log del server. Non condividerla, non salvarla altrove.
</div> </div>
<button class="secondary" onclick="loadUsers()" id="users-load-btn">Carica utenti</button> <div id="users-table-wrap" style="overflow-x:auto">
<div id="users-table-wrap" class="hidden" style="overflow-x:auto;margin-top:14px">
<table> <table>
<thead> <thead>
<tr><th>ID</th><th>Username</th><th>Indirizzo</th><th>Saldo (PLM)</th><th>Chiave</th></tr> <tr><th>ID</th><th>Username</th><th>Indirizzo</th><th>Saldo (PLM)</th><th>Chiave</th></tr>
@@ -167,12 +175,14 @@
<tbody id="users-tbody"></tbody> <tbody id="users-tbody"></tbody>
</table> </table>
</div> </div>
</div> </div>
</section>
<div id="toast-container" aria-live="polite"></div> <div id="toast-container" aria-live="polite"></div>
<script> <script>
const SATS_PER_PLM = 100000000; const SATS_PER_PLM = 100000000;
let adminToken = sessionStorage.getItem('plm_admin_token');
function toast(message, type) { function toast(message, type) {
const container = document.getElementById('toast-container'); const container = document.getElementById('toast-container');
@@ -196,7 +206,6 @@ async function withLoading(button, label, fn) {
} }
async function callAdmin(method, path, body) { async function callAdmin(method, path, body) {
const adminToken = document.getElementById('admin-token').value;
const headers = { 'Content-Type': 'application/json', 'X-Admin-Token': adminToken }; const headers = { 'Content-Type': 'application/json', 'X-Admin-Token': adminToken };
const res = await fetch(path, { method, headers, body: body ? JSON.stringify(body) : undefined }); const res = await fetch(path, { method, headers, body: body ? JSON.stringify(body) : undefined });
const data = await res.json().catch(() => ({})); const data = await res.json().catch(() => ({}));
@@ -204,19 +213,47 @@ async function callAdmin(method, path, body) {
return data; return data;
} }
async function adminLoad() { function showDashboard() {
const btn = document.getElementById('load-btn'); document.getElementById('login-section').classList.add('hidden');
await withLoading(btn, 'Caricamento…', async () => { document.getElementById('dashboard-section').classList.remove('hidden');
}
async function loadDashboard() {
await Promise.all([adminLoadConfig(), loadUsers()]);
}
async function adminLogin() {
const btn = document.getElementById('login-btn');
adminToken = document.getElementById('admin-token').value;
await withLoading(btn, 'Verifica…', async () => {
try {
await callAdmin('GET', '/admin/config');
sessionStorage.setItem('plm_admin_token', adminToken);
showDashboard();
await loadDashboard();
} catch (e) {
adminToken = null;
toast('Token non valido.', 'error');
}
});
}
function adminLogout() {
sessionStorage.removeItem('plm_admin_token');
adminToken = null;
document.getElementById('admin-token').value = '';
document.getElementById('dashboard-section').classList.add('hidden');
document.getElementById('login-section').classList.remove('hidden');
}
async function adminLoadConfig() {
try { try {
const data = await callAdmin('GET', '/admin/config'); const data = await callAdmin('GET', '/admin/config');
document.getElementById('admin-fee-address').value = data.fee_address; document.getElementById('admin-fee-address').value = data.fee_address;
document.getElementById('admin-bet-amount').value = data.bet_amount_sats / SATS_PER_PLM; document.getElementById('admin-bet-amount').value = data.bet_amount_sats / SATS_PER_PLM;
document.getElementById('admin-form').classList.remove('hidden');
toast('Configurazione caricata.', 'success');
} catch (e) { } catch (e) {
toast('Errore: ' + e.message, 'error'); toast('Errore nel caricamento configurazione: ' + e.message, 'error');
} }
});
} }
async function adminSave() { async function adminSave() {
@@ -239,8 +276,6 @@ function escapeHtml(s) {
} }
async function loadUsers() { async function loadUsers() {
const btn = document.getElementById('users-load-btn');
await withLoading(btn, 'Caricamento…', async () => {
try { try {
const users = await callAdmin('GET', '/admin/users'); const users = await callAdmin('GET', '/admin/users');
const tbody = document.getElementById('users-tbody'); const tbody = document.getElementById('users-tbody');
@@ -256,12 +291,9 @@ async function loadUsers() {
</td> </td>
</tr> </tr>
`).join(''); `).join('');
document.getElementById('users-table-wrap').classList.remove('hidden');
toast('Utenti caricati (' + users.length + ').', 'success');
} catch (e) { } catch (e) {
toast('Errore: ' + e.message, 'error'); toast('Errore nel caricamento utenti: ' + e.message, 'error');
} }
});
} }
async function revealPrivkey(userId, button) { async function revealPrivkey(userId, button) {
@@ -286,6 +318,16 @@ async function revealPrivkey(userId, button) {
} }
}); });
} }
document.getElementById('admin-token').addEventListener('keydown', (e) => {
if (e.key === 'Enter') adminLogin();
});
if (adminToken) {
callAdmin('GET', '/admin/config')
.then(() => { showDashboard(); return loadDashboard(); })
.catch(() => { sessionStorage.removeItem('plm_admin_token'); adminToken = null; });
}
</script> </script>
</body> </body>