Phase 10, part 1: users, sessions and form tokens

Schema 32 adds users, memberships and sessions, on both databases.
internal/users keeps accounts (Argon2id passwords), roles and modules,
and sessions whose rows hold only a hash of the cookie's value; they end
after a week unused or 30 days. On the server edition, every page but the
public ones needs a session and sends others to /signin. Every edition's
forms now carry a CSRF token (a cookie repeated in each form, added as
pages render; htmx sends it as a header), besides the same-origin check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 08:49:45 -07:00
co-authored by Claude Opus 5.5
parent d537fb367f
commit 96404a3580
21 changed files with 1475 additions and 21 deletions
+38
View File
@@ -52,6 +52,44 @@ CREATE TABLE locks (
taken_at TEXT NOT NULL DEFAULT '',
expires_at TEXT NOT NULL DEFAULT '' -- renewed while the work runs; free after it
);
`,
},
// 32
{
Note: "users, their memberships of the team, and their sign-in sessions (server and multi-tenant editions)",
SQLite: `
CREATE TABLE users (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL UNIQUE COLLATE NOCASE,
name TEXT NOT NULL DEFAULT '',
kind TEXT NOT NULL DEFAULT 'person', -- person or agent
status TEXT NOT NULL DEFAULT 'invited', -- invited, active or disabled
password_hash TEXT NOT NULL DEFAULT '', -- Argon2id, in the PHC string format; '' for none
title TEXT NOT NULL DEFAULT '',
time_zone TEXT NOT NULL DEFAULT '', -- an IANA name, such as Europe/Paris
photo TEXT NOT NULL DEFAULT '', -- a name in the file storage
emails TEXT NOT NULL DEFAULT '[]', -- the emails they want, as a JSON list
created_at TEXT NOT NULL,
signed_in_at TEXT NOT NULL DEFAULT '' -- their last sign-in
);
CREATE TABLE memberships (
team_id INTEGER NOT NULL DEFAULT 1, -- one team until the multi-tenant edition
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
role TEXT NOT NULL, -- owner, admin or member
modules TEXT NOT NULL DEFAULT '{}', -- a member's modules: {"seo":"edit","metrics":"view"}
created_at TEXT NOT NULL,
PRIMARY KEY (team_id, user_id)
);
CREATE TABLE sessions (
id TEXT PRIMARY KEY, -- the SHA-256 of the cookie's random ID, never the ID itself
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
created_at TEXT NOT NULL,
seen_at TEXT NOT NULL,
expires_at TEXT NOT NULL,
ip TEXT NOT NULL DEFAULT '',
user_agent TEXT NOT NULL DEFAULT ''
);
CREATE INDEX sessions_user ON sessions(user_id);
`,
},
}