Files
loki/drizzle/0080_project_invitations.sql
T
CatoandClaude Opus 5.5 ef96be9b91 feat(projects): invite anyone into a project by email, account or not (#872)
Inviting a collaborator required them to already have a Loki account.
The members route answered "No Loki account uses that email. Ask them to
register first." — a dead end with no way to ask. The only other invite
path creates a LOCAL-PASSWORD account and adds it to the inviter's whole
org: broader than one project, and a second identity path beside
OrangeCat, which fleet STACK.md says our own products must not grow.

Now any email works:

- Has an account: added at once, as before.
- No account: a one-time invite is created, emailed when mail is
  configured, and ALWAYS returned as a link to copy — an email that was
  sent is not an email that was received. They open it, sign in with
  OrangeCat (creating that account on the way), and join exactly this
  project with exactly the role chosen. No Loki password anywhere.

The link is bound to the invited email. A token is a bearer secret that
gets forwarded; on its own it would let whoever holds the link in. The
wrong account is told which address it was for, masked (ge•••@gmail.com)
because the link may be in the wrong hands.

Only a sha-256 of the token is stored, so a leaked backup cannot be
replayed as working links. Acceptance spends the invite and grants the
membership in one transaction guarded on accepted_at/revoked_at IS NULL,
so a double click or two tabs grants once. Re-inviting the same email
revokes the old link rather than leaving two live ones.

"emailed" is only reported when mail is configured AND the send
succeeded: sendEmail returns quietly when mail is off, so trusting the
call alone would have claimed an email nobody sent.

The panel moved from a collapsed "Editors" disclosure inside the
Feedback tab to the top of the project's Settings tab — where every other
product keeps "people with access", and where George looked for it. It
now offers Editor or Viewer, lists pending invites, and lets the owner
withdraw one. Editors do not see pending invitees' emails.

The accept API lives under /api/invitations/project/ so it inherits the
existing public-route exclusion instead of widening the proxy allow-list.

Migration 0080 is hand-written, like 0073-0079: drizzle/meta's snapshot
stops at 0072, so `drizzle-kit generate` diffs against a stale picture —
it believes beacon_sessions still exists and calendar_busy does not, and
would have emitted a DROP and a duplicate CREATE alongside this table.

Tests: 14 checks on the acceptance rule, proved by mutation — removing
the email binding turns the suite red. tsc clean, lint 0 errors,
check:design ok, format ok, test:unit 260/260, test:home 130/130.

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-24 02:34:14 -07:00

44 lines
2.0 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Migration: project_invitations — invite someone into ONE project by email,
-- whether or not they have an account yet.
--
-- WHY THIS EXISTS
-- Adding a collaborator required an existing Loki account; the route answered
-- "No Loki account uses that email. Ask them to register first." — a dead end
-- that left the owner to explain sign-up out of band. The only other invite
-- path (the `invitations` table) creates a LOCAL-PASSWORD account and drops it
-- into the inviter's whole org: broader than one project, and a second identity
-- path beside OrangeCat, which fleet STACK.md says our own products must not
-- grow.
--
-- This grants exactly `role` on exactly `project_id`, to whoever signs in with
-- `email`. The person arrives through OrangeCat sign-in; nothing here stores a
-- password.
--
-- ONLY A HASH OF THE TOKEN IS STORED (sha-256, hex). The raw token exists in
-- the link and nowhere else, so a leaked backup cannot be replayed as a set of
-- working invite links. (The older `invitations` table stores tokens in plain
-- text; not copied here.)
--
-- Hand-written, like 0073–0079: drizzle/meta's snapshot stops at 0072, so
-- `drizzle-kit generate` currently diffs against a stale picture — it believes
-- beacon_sessions still exists and calendar_busy does not, and would emit a
-- DROP and a duplicate CREATE alongside this table.
CREATE TABLE IF NOT EXISTS project_invitations (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
project_id uuid NOT NULL REFERENCES entities(id) ON DELETE CASCADE,
email text NOT NULL,
role text NOT NULL,
token_hash text NOT NULL UNIQUE,
invited_by uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
expires_at timestamptz NOT NULL,
accepted_by uuid REFERENCES users(id) ON DELETE SET NULL,
accepted_at timestamptz,
revoked_at timestamptz,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_project_invitations_project
ON project_invitations (project_id);
CREATE INDEX IF NOT EXISTS idx_project_invitations_email
ON project_invitations (email);