Organizations, invitation page, member management and live updates #2

Merged
lars merged 4 commits from feat/organizations into main 2026-11-24 10:46:07 +01:00
Owner

Every user now belongs to an organization, todos belong to one, and changes by others show up live.

Commits

  • Login redirect: /login?redirect= returns to the requested page (app paths only, no open redirect).
  • native-select: shadcn component, .tanstack ignored.
  • Organizations (Better Auth organization plugin)
    • Sign-up without an invitation → /onboarding creates one; with a pending invitation → joins it; a single organization is picked without a click.
    • Todos scoped by the session's active organization (requireOrganization); not a member (anymore) → 403 NO_ACTIVE_ORGANIZATION, the client moves on to another organization or /onboarding.
    • Invitation page /accept-invitation/$id: new users sign up right there (the mailed link proves the address).
    • Organization page: members (roles, remove), invitations (with role, cancel), rename, leave, delete; switcher in the header.
  • Live updates
    • A generic live_notify() Postgres trigger (member, invitation, organization, todo) → one LISTEN per server instance → live.subscribe (oRPC event iterator / SSE) → the client refetches per topic (apps/client/src/lib/live.ts). Signals only, never data.
    • One stream per browser (Web Locks + BroadcastChannel); reconnect + resync after gaps.
    • Streams get no request span / wide event; live.connected / live.disconnected logs and a short live.dispatch span per change instead. Graceful shutdown ends open streams first.
    • New live area: trigger line + topic in live/live.contract.ts + handler in lib/live.ts (see README / CLAUDE.md).

Migrations

20261123210737_organizations, 20261123221623_live (custom: trigger function). Run bun run db:migrate.

Testing

  • bun run check: typecheck, lint, format, 32 server tests (incl. live/live.test.ts)
  • bun run e2e: 9 tests (invitation flows, member management, rename/leave/delete, live across users and tabs)
  • Manually: shutdown with an open stream ends in ~70 ms, SSE keep-alive pings every 15 s under Bun
Every user now belongs to an organization, todos belong to one, and changes by others show up live. ## Commits - **Login redirect**: `/login?redirect=` returns to the requested page (app paths only, no open redirect). - **native-select**: shadcn component, `.tanstack` ignored. - **Organizations** (Better Auth organization plugin) - Sign-up without an invitation → `/onboarding` creates one; with a pending invitation → joins it; a single organization is picked without a click. - Todos scoped by the session's active organization (`requireOrganization`); not a member (anymore) → 403 `NO_ACTIVE_ORGANIZATION`, the client moves on to another organization or `/onboarding`. - Invitation page `/accept-invitation/$id`: new users sign up right there (the mailed link proves the address). - Organization page: members (roles, remove), invitations (with role, cancel), rename, leave, delete; switcher in the header. - **Live updates** - A generic `live_notify()` Postgres trigger (member, invitation, organization, todo) → one `LISTEN` per server instance → `live.subscribe` (oRPC event iterator / SSE) → the client refetches per topic (`apps/client/src/lib/live.ts`). Signals only, never data. - One stream per browser (Web Locks + BroadcastChannel); reconnect + resync after gaps. - Streams get no request span / wide event; `live.connected` / `live.disconnected` logs and a short `live.dispatch` span per change instead. Graceful shutdown ends open streams first. - New live area: trigger line + topic in `live/live.contract.ts` + handler in `lib/live.ts` (see README / CLAUDE.md). ## Migrations `20261123210737_organizations`, `20261123221623_live` (custom: trigger function). Run `bun run db:migrate`. ## Testing - `bun run check`: typecheck, lint, format, 32 server tests (incl. `live/live.test.ts`) - `bun run e2e`: 9 tests (invitation flows, member management, rename/leave/delete, live across users and tabs) - Manually: shutdown with an open stream ends in ~70 ms, SSE keep-alive pings every 15 s under Bun
Takes ?redirect=, restricted to app paths (no open redirect).
- Better Auth organization plugin. Every user belongs to an organization:
  sign-up without an invitation -> /onboarding creates one, with a pending
  invitation -> joins it; a single organization is picked without a click.
- Todos are scoped by the session's active organization (requireOrganization).
  Not a member (anymore) -> 403 NO_ACTIVE_ORGANIZATION, the client moves on
  to another organization or /onboarding.
- Invitation page /accept-invitation/$id: shows the invitation, new users sign
  up right there (no confirmation mail, the mailed link proves the address).
- Organization page: members (roles, remove), invitations (with role, cancel),
  rename, leave, delete; organization switcher in the header.
feat: live updates via Postgres NOTIFY and oRPC event streams
Some checks failed
ci / check (pull_request) Failing after 1m1s
ci / e2e (pull_request) Failing after 3m54s
18b0d68b22
Changes by others show up without a reload: roles and permissions, members,
invitations, organizations and todos. A removed member is moved on right away.

- DB: a generic live_notify(topic, org column [, user column]) trigger on
  member, invitation, organization and todo; delivered on commit.
- Server (live/): one LISTEN connection per instance, fanned out to the open
  streams of the affected users (their organizations + their own memberships).
  Signals only ({ topic, organizationId }), never data.
- Transport: live.subscribe, an oRPC event iterator (SSE). One stream per
  browser: one tab holds it (Web Locks) and forwards events (BroadcastChannel).
- Client (lib/live.ts): what to refetch per topic; reconnect + resync after gaps.
  Replaces the polling on the organization page.
- Observability: streams get no request span / wide event; instead
  live.connected / live.disconnected logs and a short live.dispatch span per
  change. Graceful shutdown ends open streams first.
lars merged commit c5173228a9 into main 2026-11-24 10:46:07 +01:00
lars deleted branch feat/organizations 2026-11-24 10:46:07 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
hampe.work/app-starter!2
No description provided.