Repository navigation
Description
Parent
Auth: Clerk, companies and machine tokens on tier 0, spec §7, §8 and §11.
What to build
A person signs in and lands in a company. Session verifies Clerk's cookie locally and completes the handshake; RequireSession puts a Viewer in front of every first-party page; create-or-join is Companies' first server-rendered page. A developer with PATCHY_DEV_CLERK_USER_ID in dev.env signs in to their local instance and is Patchy Dev's admin; without it they land on /join and create a company. Sign-out works from create-or-join and the deactivated page. No /d/* route changes yet; the company page and invitations are the next ticket.
Do
Sessionin Auth over@clerk/backend(add the dependency):authenticate(request)answering signed-in (sub,email,name,sid), signed-out, or a handshake response to return as-is;revoke(sid);signOutCookies()naming every Clerk cookie from the SDK's constants with the setter'sDomainandPath;frontendApiHostderived from the publishable key. The Request handed to the SDK is built fromPATCHY_PUBLIC_BASE_URLwithhostandx-forwarded-*pinned from it. OnlySet-Cookieand a handshakeLocationare forwarded from Clerk;x-clerk-auth-*and theAccess-Control-*pair never reach a response. A handshake that returns still signed out renders sign-in could not complete with the reason in the server log, never a loop.- Config:
CLERK_PUBLISHABLE_KEYandCLERK_SECRET_KEYrequired at startup;PATCHY_PUBLIC_BASE_URLloses its default;CLERK_JWT_KEYoptional, validated withcreatePublicKeyat boot;CLERK_AUTHORIZED_PARTIESoptional, one origin. The server refuses to start without the required four, naming them. RequireSessionmiddleware in Auth providingViewer(user, company, role, fromUsers.findByClerkIdwith the claim refresh): no session answers 401 with the door page; a session with no user row answers 303 to/joinwith thereturnpath; a deactivated user gets the deactivated page (403,no-store, with a Sign out control). The Origin check on every first-partyPOST(403 on a foreign or missingOrigin).- The shell moves from Serving's
render.tstocore(htmlPage,escapeHtml,escapeAttribute); Serving's home, 404 and patch wrapper keep rendering as they do. First-party pages behind the session carry the doored CSP (script-srcandconnect-srcfor the Frontend API host,form-action 'self') and load Clerk's headless script so the token stays fresh. - Pages:
/login(the door as a page, one Sign in link to the Account Portal withredirect_url= the validatedreturn, default/joinuntil Your machines exists);/joinGET and POST (create-or-join exactly as spec §8: the email named, every live invite for it with its own Join, or the create form with the handle pre-filled and editable, each in one transaction with the user row; Not you? Sign out on the page);POST /logout(revoke the Clerk session, clear every Clerk cookie with the setter'sDomainandPath, 303 to/login; needs a Clerk session and nothing else)./joinfor a person already in a company sends them to theirreturnor/join's own "you are in<Company>" line until/companyexists. @patchy/auth/testing: the RSA-2048 keypair,clerkEnv()(fake keys with a Frontend API host under.invalid, the PEM), the session signer, the fetch guard on import. The dev runner readsPATCHY_DEV_CLERK_USER_IDfromdev.envand seeds it as the admin's Clerk id;CLERK_JWT_KEYsurvivesdev.envquoted.- Tests:
Sessionagainst the keypair for signed-in, signed-out, handshake, both key shapes and thepk_live_shape, with zero network;/login,/joinand/logoutin memory and on a socket with hops driven by hand; create-or-join's refusals (already in a company, handle taken, reserved, malformed); two sign-ins consuming the same invite create one user; sign-out leaves no Clerk cookie in the jar and works with no user row. - Docs:
docs/DEVELOPMENT.md(the four required variables,CLERK_JWT_KEYas test-facing,PATCHY_DEV_CLERK_USER_ID, what a local sign-in lands on), thepatchy-dev-loopskill's env line, the packed e2e starting its server with fake keys. Glossaries: Auth gains Session (rewritten), Viewer; Companies gains Create-or-join;packages/serving/CONTEXT.md's Page notes the shell's new home.
Surfaces
The dev loop; vocabulary and product shape; served pages (ui-consistency on every new page); reverse states (sign-out beside sign-in, reachable from every session state).
Done when
- On a
pnpm devinstance withdev.envkeys:/joinsigned out answers 401 with the door; after a Google sign-in withPATCHY_DEV_CLERK_USER_IDset,/joinsays the developer is in Patchy Dev; without it,/joincreates a company and the next/joinsays so. - Not you? Sign out on
/joinlands on/loginand the next/joinis the door. -
pnpm teststays offline: the fetch guard records no request. - The server refuses to start without any of the four required variables, naming it.
-
pnpm test,pnpm typecheck,pnpm lintand the packed e2e pass.
Decisions implemented
The login door on a tier 0 page (the session, config, the origin, first-party pages); Company and user (create-or-join, the user row's birth); The dev loop and tests with Clerk; the research on Clerk on a server with no frontend, Clerk sign-in as decided and Effect 4 HTTP for the door; the evidence on the login door end to end; ADR-0005 and ADR-0006; spec §7, §8, §11. The prototype branch prototype/login-door shows a working Session and keypair fixture; copy the shape, not the files.
Activity
[-]feat(auth): the Clerk session, create-or-join and the company page[/-][+]feat(auth): sign in with Clerk and create a company[/+]