Admin: delete an account, not just disable it (#107)

Disabling locks someone out but deliberately leaves their state file and credential on disk, so
'no data remains' was not reachable from the dashboard — which is what the request asked for.

POST /api/admin/user/delete removes the user, their passkeys, their push subscriptions, their
presence entry, their training history and any Coach credential of theirs. Two refusals, because
nothing in the UI could undo either: an admin cannot delete their own account, and the last
remaining admin cannot be removed. The invite code they joined with stays burned — it was used,
and freeing it would quietly widen an invite-only instance.

In the dashboard it asks twice, naming the account both times, and offers their data as a
download first: the training history is theirs. Five tests drive the real server.
This commit is contained in:
Duarte Santos committed 2026-09-20 21:04:56 +02:00
1 parent ae1c8fe8b2
commit 90a1e8956a
4 files changed
+179

No files matched your search

+1
View File
@@ -28,6 +28,7 @@ const LABELS = {
'auth.pair.fail': 'Pairing failed',
'admin.user.disable': 'Disabled an account',
'admin.user.enable': 'Re-enabled an account',
'admin.user.delete': 'Deleted an account',
'admin.invite.create': 'Created an invite code',
'admin.invite.revoke': 'Revoked an invite code',
'admin.audit.clear': 'Cleared the activity log',
+33
View File
@@ -42,6 +42,20 @@ function UserDetail({ id, onChanged, close }) {
// left exactly this account un-disableable. setsDone/workoutVolume walk entries and sets, so
// an entry that lacks either has nothing to show and is skipped rather than drawn.
const workouts = (d.workouts || []).filter(w => w && Array.isArray(w.entries) && w.entries.every(e => e && Array.isArray(e.sets)))
// Their whole record as the admin API already returns it — the export the delete sheet offers.
const exportUser = () => {
const blob = new Blob([JSON.stringify(d, null, 2)], { type: 'application/json' })
const a = document.createElement('a')
a.href = URL.createObjectURL(blob)
a.download = `opengym-${u.name.replace(/[^a-zA-Z0-9_-]+/g, '-').toLowerCase()}-${u.id}.json`
document.body.appendChild(a); a.click(); a.remove()
setTimeout(() => URL.revokeObjectURL(a.href), 1000)
}
const doDelete = () => {
api('/api/admin/user/delete', { method: 'POST', body: JSON.stringify({ id: u.id }) })
.then(() => { toast('Account deleted'); onChanged(); close() })
.catch(e => toast(e.message))
}
const setDisabled = disabled => {
api('/api/admin/user/disable', { method: 'POST', body: JSON.stringify({ id: u.id, disabled }) })
.then(() => { toast(disabled ? 'User disabled' : 'User enabled'); onChanged(); close() })
@@ -67,6 +81,25 @@ function UserDetail({ id, onChanged, close }) {
: confirmSheet({ title: 'Disable ' + u.name + '?', message: 'They are signed out everywhere and can no longer sync or log in until re-enabled. Their data stays.', confirmText: 'Disable', danger: true, onConfirm: () => setDisabled(true) })}>
{u.disabled ? 'Enable account' : 'Disable account'}</button>
<div className="adm-hint">{u.disabled ? 'Enabling lets them sign in and sync again.' : 'Disabling signs them out everywhere and blocks sign-in. Nothing is deleted.'}</div>
{/* The one destructive action in the app (issue #107), so it asks twice and offers the
export first — that history is theirs. The second step names the account again, because
the first sheet can be dismissed by anyone who was not reading. */}
<button className="btn danger" style={{ margin: '14px 0 4px' }}
onClick={() => confirmSheet({
title: 'Delete ' + u.name + '?',
message: 'Everything goes: their workouts, weigh-ins, routines, passkeys and notifications. This cannot be undone, and the invite code they joined with stays used. Download their data first if they might want it.',
confirmText: 'Continue',
danger: true,
onConfirm: () => confirmSheet({
title: 'Delete ' + u.name + ' for good?',
message: 'Last chance — there is no undo and no backup of this on the server.',
confirmText: 'Delete account',
danger: true,
onConfirm: doDelete,
}),
})}>Delete account</button>
<button className="btn" style={{ marginBottom: 4 }} onClick={exportUser}>Download their data</button>
<div className="adm-hint">Deleting removes the account and every trace of its training history from this server.</div>
</>}
<h4 className="sec">Workout history</h4>
{workouts.length ? <div className="list" style={{ gap: 0 }}>