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
+106 -64
+106 -64
View File
@@ -124,55 +124,65 @@
</head> </head>
<body> <body>
<header> <section id="login-section">
<h1>PLM Lottery — Admin</h1> <header>
<p>Configurazione operativa del round</p> <h1>PLM Lottery — Admin</h1>
</header> <p>Accesso riservato</p>
</header>
<div class="card"> <div class="card">
<p class="hint"> <label for="admin-token">Admin token</label>
Salvata nel database, modificabile in qualsiasi momento senza riavviare il server. <input id="admin-token" type="password" placeholder="valore di ADMIN_TOKEN" autofocus>
Serve il token admin (<code>ADMIN_TOKEN</code> nel <code>.env</code> del server). <button onclick="adminLogin()" id="login-btn">Accedi</button>
</p> </div>
</section>
<label for="admin-token">Admin token</label> <section id="dashboard-section" class="hidden">
<input id="admin-token" type="password" placeholder="valore di ADMIN_TOKEN"> <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>
<button class="secondary" onclick="adminLoad()" id="load-btn">Carica configurazione attuale</button> <div class="card">
<h2 style="font-size:1rem;font-weight:600;margin:0 0 4px">Configurazione</h2>
<p class="hint">
Salvata nel database, modificabile in qualsiasi momento senza riavviare il server.
</p>
<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>
<div class="warning-banner"> <div class="warning-banner">
⚠ 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 id="users-table-wrap" style="overflow-x:auto">
<table>
<thead>
<tr><th>ID</th><th>Username</th><th>Indirizzo</th><th>Saldo (PLM)</th><th>Chiave</th></tr>
</thead>
<tbody id="users-tbody"></tbody>
</table>
</div>
</div> </div>
</section>
<button class="secondary" onclick="loadUsers()" id="users-load-btn">Carica utenti</button>
<div id="users-table-wrap" class="hidden" style="overflow-x:auto;margin-top:14px">
<table>
<thead>
<tr><th>ID</th><th>Username</th><th>Indirizzo</th><th>Saldo (PLM)</th><th>Chiave</th></tr>
</thead>
<tbody id="users-tbody"></tbody>
</table>
</div>
</div>
<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,21 +213,49 @@ 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 { try {
const data = await callAdmin('GET', '/admin/config'); await callAdmin('GET', '/admin/config');
document.getElementById('admin-fee-address').value = data.fee_address; sessionStorage.setItem('plm_admin_token', adminToken);
document.getElementById('admin-bet-amount').value = data.bet_amount_sats / SATS_PER_PLM; showDashboard();
document.getElementById('admin-form').classList.remove('hidden'); await loadDashboard();
toast('Configurazione caricata.', 'success');
} catch (e) { } catch (e) {
toast('Errore: ' + e.message, 'error'); 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 {
const data = await callAdmin('GET', '/admin/config');
document.getElementById('admin-fee-address').value = data.fee_address;
document.getElementById('admin-bet-amount').value = data.bet_amount_sats / SATS_PER_PLM;
} catch (e) {
toast('Errore nel caricamento configurazione: ' + e.message, 'error');
}
}
async function adminSave() { async function adminSave() {
const btn = document.getElementById('save-btn'); const btn = document.getElementById('save-btn');
const feeAddress = document.getElementById('admin-fee-address').value; const feeAddress = document.getElementById('admin-fee-address').value;
@@ -239,29 +276,24 @@ function escapeHtml(s) {
} }
async function loadUsers() { async function loadUsers() {
const btn = document.getElementById('users-load-btn'); try {
await withLoading(btn, 'Caricamento…', async () => { const users = await callAdmin('GET', '/admin/users');
try { const tbody = document.getElementById('users-tbody');
const users = await callAdmin('GET', '/admin/users'); tbody.innerHTML = users.map((u) => `
const tbody = document.getElementById('users-tbody'); <tr>
tbody.innerHTML = users.map((u) => ` <td>${u.id}</td>
<tr> <td>${escapeHtml(u.username)}</td>
<td>${u.id}</td> <td class="addr">${escapeHtml(u.address)}</td>
<td>${escapeHtml(u.username)}</td> <td>${u.balance_sats / SATS_PER_PLM}</td>
<td class="addr">${escapeHtml(u.address)}</td> <td>
<td>${u.balance_sats / SATS_PER_PLM}</td> <button class="reveal" onclick="revealPrivkey(${u.id}, this)">Mostra</button>
<td> <div class="privkey-box hidden" id="privkey-${u.id}"></div>
<button class="reveal" onclick="revealPrivkey(${u.id}, this)">Mostra</button> </td>
<div class="privkey-box hidden" id="privkey-${u.id}"></div> </tr>
</td> `).join('');
</tr> } catch (e) {
`).join(''); toast('Errore nel caricamento utenti: ' + e.message, 'error');
document.getElementById('users-table-wrap').classList.remove('hidden'); }
toast('Utenti caricati (' + users.length + ').', 'success');
} catch (e) {
toast('Errore: ' + 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>