Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

FIRST_USER_ADMIN is opt-in (#328)

Duarte Santosbb41bea1c7ec
diff --git a/.env.example b/.env.example
index fab0f2234..d9ff57134 100644
--- a/.env.example
+++ b/.env.example
@@ -27,9 +27,9 @@ RP_NAME=openGym
# "First" means: the instance had no profiles at all when it registered. An
# instance that already has profiles promotes nobody when you upgrade, and an
# instance whose profiles were all deleted counts as fresh again. Register your
-# own profile before you share the address. To turn it off:
+# own profile before you share the address. Off by default; to turn it on:
#
-# FIRST_USER_ADMIN=0
+# FIRST_USER_ADMIN=1
#
# More admins, or admins on an instance that existed before this setting: find
# the user id in Settings -> Account -> Account ID (or ./data/db.json,
diff --git a/README.md b/README.md
index 1986d083d..eb11752ac 100644
--- a/README.md
+++ b/README.md
@@ -150,7 +150,7 @@ Traefik and nginx, and there are separate guides for
| `RP_NAME` | Name shown in the passkey prompt | `openGym` |
| `SESSION_DAYS` | How long a sign-in lasts, in days | `90` |
| `ADMIN_UIDS` | User ids that get the admin dashboard, comma-separated | *(none)* |
-| `FIRST_USER_ADMIN` | The first profile created on an empty instance becomes its admin; `0` turns it off | *(on)* |
+| `FIRST_USER_ADMIN` | `1`: the first profile created on an empty instance becomes its admin | *(off)* |
| `INVITE_ONLY` | Require an invite code to create a profile | *(off)* |
| `ALLOW_GUEST` | Offer "Continue without account"; `0` requires a profile | *(on)* |
| `PASSWORD_LOGIN` | Offer name-and-password sign-in next to passkeys | *(off)* |
diff --git a/api/openapi.yaml b/api/openapi.yaml
index 11f51d8b1..6c1a11e4b 100644
--- a/api/openapi.yaml
+++ b/api/openapi.yaml
@@ -75,7 +75,7 @@ info:
this API at all).
- `ADMIN_UIDS=<uid>,<uid>` — user ids treated as admins (a `"admin": true` flag on
the user record in `db.json` works too). Admins get the `/api/admin/*` routes.
- - `FIRST_USER_ADMIN` (default on; `0` turns it off) — the first profile registered
+ - `FIRST_USER_ADMIN` (default off; `1` turns it on) — the first profile registered
on an instance with no profiles gets that `"admin": true` flag. Decided at
registration only, never applied to an instance that already has profiles.
- `AUDIT_LOG` (default on), `AUDIT_MAX` (default 5000 events), `AUDIT_DAYS`
diff --git a/api/server.js b/api/server.js
index 3bf362d04..275a8b34e 100644
--- a/api/server.js
+++ b/api/server.js
@@ -47,8 +47,10 @@ const INVITE_ONLY = /^(1|true|yes|on)$/i.test(process.env.INVITE_ONLY || '');
// fresh install has someone who can open the admin dashboard without editing ADMIN_UIDS and
// restarting. Stored on the user record (user.admin), which isAdmin already honours. It is
// decided at the moment of registration only: an instance that already has profiles changes
-// nothing on upgrade. Default ON; FIRST_USER_ADMIN=0 turns it off.
-const FIRST_USER_ADMIN = !/^(0|false|no|off)$/i.test(process.env.FIRST_USER_ADMIN || '');
+// nothing on upgrade. Default OFF: an existing instance with no profiles yet (guest-only, or phones
+// kept local) would otherwise hand admin to whoever signs up first after the upgrade.
+// FIRST_USER_ADMIN=1 turns it on for a fresh install.
+const FIRST_USER_ADMIN = /^(1|true|yes|on)$/i.test(process.env.FIRST_USER_ADMIN || '');
// Checked synchronously right before db.users.push, with no await in between, so two
// registrations racing each other cannot both see an empty instance.
function claimFirstAdmin(req, user) {
diff --git a/api/test/server-first-user-admin.test.js b/api/test/server-first-user-admin.test.js
index 988604818..8edec5cb2 100644
--- a/api/test/server-first-user-admin.test.js
+++ b/api/test/server-first-user-admin.test.js
@@ -1,6 +1,6 @@
/* FIRST_USER_ADMIN (#328): the first profile made on an instance with no profiles becomes its
admin, through either way of registering. Never retroactive — an instance that already has
- profiles changes nothing — and FIRST_USER_ADMIN=0 turns it off. Real server.js in a child, the
+ profiles changes nothing. Off unless FIRST_USER_ADMIN=1. Real server.js in a child, the
same harness as server-passkeys.test.js. */
import { test, before } from 'node:test';
import assert from 'node:assert/strict';
@@ -124,7 +124,7 @@ async function registerPasskey(h, name, ip) {
const stored = (h, id) => h.db().users.find(u => u.id === id);
test('the first password registration on an empty instance is the admin, the second is not', async t => {
- const h = await startServer(t);
+ const h = await startServer(t, { env: { FIRST_USER_ADMIN: '1' } });
const first = await registerPassword(h, 'Ana', '198.51.100.10');
assert.equal(first.status, 200);
assert.equal(first.body.user.admin, true);
@@ -140,7 +140,7 @@ test('the first password registration on an empty instance is the admin, the sec
});
test('the first passkey registration on an empty instance is the admin, the second is not', async t => {
- const h = await startServer(t);
+ const h = await startServer(t, { env: { FIRST_USER_ADMIN: '1' } });
const first = await registerPasskey(h, 'Ana', '198.51.100.20');
assert.equal(first.status, 200);
assert.equal(first.body.user.admin, true);
@@ -151,8 +151,8 @@ test('the first passkey registration on an empty instance is the admin, the seco
assert.equal('admin' in stored(h, second.body.user.id), false);
});
-test('FIRST_USER_ADMIN=0 leaves the first profile an ordinary one', async t => {
- for (const off of ['0', 'false', 'off']) {
+test('unset or FIRST_USER_ADMIN=0 leaves the first profile an ordinary one', async t => {
+ for (const off of [undefined, '', '0', 'false', 'off']) {
const h = await startServer(t, { env: { FIRST_USER_ADMIN: off } });
const r = await registerPassword(h, 'Ana', '198.51.100.30');
assert.equal(r.status, 200);
@@ -164,7 +164,7 @@ test('FIRST_USER_ADMIN=0 leaves the first profile an ordinary one', async t => {
});
test('an instance that already has profiles promotes nobody on upgrade or on a new registration', async t => {
- const h = await startServer(t, { users: [user('u1', 'Old'), user('u2', 'Older')] });
+ const h = await startServer(t, { env: { FIRST_USER_ADMIN: '1' }, users: [user('u1', 'Old'), user('u2', 'Older')] });
assert.equal(stored(h, 'u1').admin, undefined);
const r = await registerPassword(h, 'New', '198.51.100.40');
assert.equal(r.status, 200);
@@ -175,7 +175,7 @@ test('an instance that already has profiles promotes nobody on upgrade or on a n
});
test('an ADMIN_UIDS admin stays an admin, and does not make the next profile one', async t => {
- const h = await startServer(t, { env: { ADMIN_UIDS: 'u1' }, users: [user('u1', 'Owner')] });
+ const h = await startServer(t, { env: { ADMIN_UIDS: 'u1', FIRST_USER_ADMIN: '1' }, users: [user('u1', 'Owner')] });
assert.equal((await h.req('GET', '/api/admin/users', { cookie: mintSession('u1') })).status, 200);
const r = await registerPassword(h, 'Guest', '198.51.100.50');
assert.equal(r.body.user.admin, false);
diff --git a/docs/SELF_HOSTING.md b/docs/SELF_HOSTING.md
index 8e45a7698..e8dcea5e3 100644
--- a/docs/SELF_HOSTING.md
+++ b/docs/SELF_HOSTING.md
@@ -154,8 +154,10 @@ the request is refused, pairing says so instead of "Failed to fetch".
## 4. Multiple users
Anyone who can reach the URL can create their own profile — each gets isolated data. That's the
-default: open signup, and **the first profile created on the instance is its admin**
-(`FIRST_USER_ADMIN`, on by default). So register your own profile before you share the address.
+default: open signup. With `FIRST_USER_ADMIN=1` in `.env`, **the first profile created on the
+instance becomes its admin**, so a fresh install needs no `ADMIN_UIDS` edit. It is off by default,
+because an existing instance with no profiles yet would hand admin to whoever signs up first; if
+you turn it on, register your own profile before you share the address.
"First" is decided at the moment of registration: the instance had no profiles at all. It is
never applied after the fact — an instance that already has profiles promotes nobody when it is
@@ -169,7 +171,7 @@ If you'd rather control who gets in, these optional settings in `.env` turn that
ADMIN_UIDS=youruserid # comma-separated; these users get the admin dashboard too
INVITE_ONLY=1 # new profiles need an invite code
ALLOW_GUEST=0 # remove "Continue without account"
-FIRST_USER_ADMIN=0 # the first profile is an ordinary one; admins only via ADMIN_UIDS
+FIRST_USER_ADMIN=1 # the first profile on an empty instance becomes its admin
```
Create your own profile before you switch `INVITE_ONLY` on: with no profile and no admin yet,
diff --git a/kubernetes/deployment.yaml b/kubernetes/deployment.yaml
index 4694c9dc4..6c89a26d6 100644
--- a/kubernetes/deployment.yaml
+++ b/kubernetes/deployment.yaml
@@ -58,8 +58,8 @@ spec:
value: https://opengym.example.com
# Everything else in .env.example (INVITE_ONLY, ADMIN_UIDS, FIRST_USER_ADMIN,
# ALLOW_GUEST, DEFAULT_LANG, PASSWORD_LOGIN, the push keys, ...) goes here the
- # same way. FIRST_USER_ADMIN is on unless set to "0": the first profile created on
- # an empty instance becomes its admin.
+ # same way. FIRST_USER_ADMIN="1" makes the first profile created on an empty
+ # instance its admin (off by default).
# TRUST_PROXY is left off on purpose: the web container overwrites X-Forwarded-For
# with the address it was reached from, which here is the gateway's pod, so turning
# it on changes nothing for the sign-in throttle — while any pod in the cluster that
diff --git a/website/api.html b/website/api.html
index 880e58a94..2258e06da 100644
--- a/website/api.html
+++ b/website/api.html
@@ -275,7 +275,7 @@ main.api-main .lead { color: var(--fg-2); max-width: 58ch; margin: 14px 0 0; fon
</div>
<div class="ov">
<h3>Environment-dependent behavior</h3>
-<ul><li><code>INVITE_ONLY=1</code> — registration requires a valid invite code (minted by an admin).</li><li><code>ALLOW_GUEST=0</code> — hides the client-side &quot;continue without account&quot; mode; the server merely reports the flag via <code>GET /api/config</code> (guest mode never talks to this API at all).</li><li><code>ADMIN_UIDS=&lt;uid&gt;,&lt;uid&gt;</code> — user ids treated as admins (a <code>&quot;admin&quot;: true</code> flag on the user record in <code>db.json</code> works too). Admins get the <code>/api/admin/*</code> routes.</li><li><code>FIRST_USER_ADMIN</code> (default on; <code>0</code> turns it off) — the first profile registered on an instance with no profiles gets that <code>&quot;admin&quot;: true</code> flag. Decided at registration only, never applied to an instance that already has profiles.</li><li><code>AUDIT_LOG</code> (default on), <code>AUDIT_MAX</code> (default 5000 events), <code>AUDIT_DAYS</code> (default 90), <code>AUDIT_IP</code> (<code>off</code> | <code>net</code> | <code>full</code>, default off) — shape the audit log served by <code>GET /api/admin/audit</code>.</li><li><code>PASSWORD_LOGIN=1</code> — adds the <code>password</code> routes and <code>password_login: true</code> in <code>GET /api/config</code>. Off by default.</li><li><code>DEFAULT_LANG</code> (e.g. <code>pt-BR</code>; unset by default) — <code>default_lang</code> in <code>GET /api/config</code>, the language of the sign-in screen and of every profile that never picked one.</li><li><code>TRUST_PROXY=1</code> — the throttle reads the client address from proxy headers (set by the bundled <code>docker-compose.yml</code>, where only the web container can reach the API).</li><li><code>MEDIA_UPLOADS</code> (default on; <code>0</code> removes the <code>media</code> routes and the <code>media</code> block of <code>GET /api/config</code>), <code>MEDIA_QUOTA_MB</code> (200 per profile, <code>0</code> = no cap), <code>MEDIA_IMAGE_MAX_MB</code> (2), <code>MEDIA_GIF_MAX_MB</code> (8), <code>MEDIA_VIDEO_MAX_MB</code> (40), <code>MEDIA_VIDEO_MAX_SEC</code> (60), <code>MEDIA_GC_GRACE_DAYS</code> (14), <code>MEDIA_UPLOADS_PER_HOUR</code> (600 per profile), <code>MEDIA_MIN_FREE_MB</code> (512, <code>0</code> = no floor) — photos and videos of custom exercises (tag <code>media</code>). Every MB here is 2^20 bytes.</li></ul>
+<ul><li><code>INVITE_ONLY=1</code> — registration requires a valid invite code (minted by an admin).</li><li><code>ALLOW_GUEST=0</code> — hides the client-side &quot;continue without account&quot; mode; the server merely reports the flag via <code>GET /api/config</code> (guest mode never talks to this API at all).</li><li><code>ADMIN_UIDS=&lt;uid&gt;,&lt;uid&gt;</code> — user ids treated as admins (a <code>&quot;admin&quot;: true</code> flag on the user record in <code>db.json</code> works too). Admins get the <code>/api/admin/*</code> routes.</li><li><code>FIRST_USER_ADMIN</code> (default off; <code>1</code> turns it on) — the first profile registered on an instance with no profiles gets that <code>&quot;admin&quot;: true</code> flag. Decided at registration only, never applied to an instance that already has profiles.</li><li><code>AUDIT_LOG</code> (default on), <code>AUDIT_MAX</code> (default 5000 events), <code>AUDIT_DAYS</code> (default 90), <code>AUDIT_IP</code> (<code>off</code> | <code>net</code> | <code>full</code>, default off) — shape the audit log served by <code>GET /api/admin/audit</code>.</li><li><code>PASSWORD_LOGIN=1</code> — adds the <code>password</code> routes and <code>password_login: true</code> in <code>GET /api/config</code>. Off by default.</li><li><code>DEFAULT_LANG</code> (e.g. <code>pt-BR</code>; unset by default) — <code>default_lang</code> in <code>GET /api/config</code>, the language of the sign-in screen and of every profile that never picked one.</li><li><code>TRUST_PROXY=1</code> — the throttle reads the client address from proxy headers (set by the bundled <code>docker-compose.yml</code>, where only the web container can reach the API).</li><li><code>MEDIA_UPLOADS</code> (default on; <code>0</code> removes the <code>media</code> routes and the <code>media</code> block of <code>GET /api/config</code>), <code>MEDIA_QUOTA_MB</code> (200 per profile, <code>0</code> = no cap), <code>MEDIA_IMAGE_MAX_MB</code> (2), <code>MEDIA_GIF_MAX_MB</code> (8), <code>MEDIA_VIDEO_MAX_MB</code> (40), <code>MEDIA_VIDEO_MAX_SEC</code> (60), <code>MEDIA_GC_GRACE_DAYS</code> (14), <code>MEDIA_UPLOADS_PER_HOUR</code> (600 per profile), <code>MEDIA_MIN_FREE_MB</code> (512, <code>0</code> = no floor) — photos and videos of custom exercises (tag <code>media</code>). Every MB here is 2^20 bytes.</li></ul>
</div>
<div class="ov">
<h3>Conventions</h3>