Files
printcraft/scripts/db/accountkit-import.sql
CatoandClaude Opus 5.5 b40e67aeea feat(auth): every way in on AccountKit's own screens (#91)
* feat(auth): every way in on AccountKit's own screens

PrintCraft signed people in against OrangeCat's GoTrue (auth.users). It now
runs on AccountKit (@bitbaum/accountkit 0.23) with AccountKit's own forms:
/login offers email and password, an emailed sign-in link, a passkey,
recovery through the mailed link and the second factor; /register is
sign-up; /account carries the account panel. "Continue with OrangeCat" and
the shared sign-in show when their clients are set, the link and recovery
when mail is. The GoTrue forms, the Supabase cookie middleware and the
browser auth client are gone.

AccountKit's tables live in a host accounts database (app/drizzle, loki
accounts-db.conf); the project data stays in the printcraft supabase schema.
API routes always read it through the service-role client and check
ownership against the session's user id, as they already did for guests.
New accounts get uuid ids (projects.user_id is a uuid), and a supabase
migration drops the projects.user_id key to auth.users, which a new
person's id is not in. Nothing is deleted.

Existing people keep their projects: scripts/db/accountkit-people.sql lists
every project owner with their OrangeCat actor id, and
scripts/db/accountkit-import.sql gives each an account under the same id,
linked to OrangeCat by that actor id, never by address (loki
accountkit-supabase.yml rehearses it rolled back, then imports).

src/lib/auth/accounts.test.ts runs the ways in and the import on the
committed migration; accounts.yml rehearses the import and runs the browser
journey against the production build with AccountKit's real mail caught by
a sink.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

* ci(auth): the account journey is the browser smoke the rollout gate reads

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

* docs(ci): name the journey's workflow

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-02 22:01:52 +01:00

42 lines
2.4 KiB
SQL

-- PrintCraft's people from OrangeCat's GoTrue into AccountKit, in the accounts
-- database. Runs inside one transaction (loki accountkit-supabase.sh: rolled
-- back for a rehearsal, committed for the import) with oc_people(user_id uuid,
-- actor_id text) filled from scripts/db/accountkit-people.sql.
--
-- Each person who owns projects and has an OrangeCat actor gets:
-- - an AccountKit user under their OLD id, so projects.user_id needs no
-- rewrite, with the placeholder address AccountKit gives every OrangeCat
-- sign-in (placeholderEmail("orangecat", actorId):
-- `<first 32 hex of sha256("orangecat:<actor>")>@orangecat.invalid`);
-- - an `orangecat` account keyed by the actor id, which is the OIDC `sub`,
-- so their first "Continue with OrangeCat" lands on that user.
-- No GoTrue password or address is carried over: OrangeCat's password sign-up
-- does not verify an address, so an address is never an identity here.
-- Idempotent: a person already imported, or an actor already linked, is left
-- as it is and counted. Nothing is updated or deleted.
WITH eligible AS (
SELECT user_id::text AS uid, actor_id FROM oc_people WHERE actor_id IS NOT NULL
), ins_users AS (
INSERT INTO users (id, name, email, email_verified)
SELECT e.uid, '',
substr(encode(sha256(convert_to('orangecat:' || e.actor_id, 'UTF8')), 'hex'), 1, 32) || '@orangecat.invalid',
false
FROM eligible e
WHERE NOT EXISTS (SELECT 1 FROM accounts a WHERE a.provider_id = 'orangecat' AND a.account_id = e.actor_id)
ON CONFLICT DO NOTHING
RETURNING id
), ins_accounts AS (
INSERT INTO accounts (id, account_id, provider_id, user_id)
SELECT gen_random_uuid()::text, e.actor_id, 'orangecat', e.uid
FROM eligible e JOIN ins_users i ON i.id = e.uid
RETURNING id
)
SELECT
'imported_users=' || (SELECT count(*) FROM ins_users),
'imported_orangecat_links=' || (SELECT count(*) FROM ins_accounts),
'already_linked=' || (SELECT count(*) FROM eligible e WHERE EXISTS (
SELECT 1 FROM accounts a WHERE a.provider_id = 'orangecat' AND a.account_id = e.actor_id AND a.user_id = e.uid)),
'actor_linked_to_another_account=' || (SELECT count(*) FROM eligible e WHERE EXISTS (
SELECT 1 FROM accounts a WHERE a.provider_id = 'orangecat' AND a.account_id = e.actor_id AND a.user_id <> e.uid)),
'without_actor=' || (SELECT count(*) FROM oc_people WHERE actor_id IS NULL);