Skip to content

feat(auth): sign in with Clerk and create a company #138

Closed

Description

@allisonmahmood

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

  • Session in 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's Domain and Path; frontendApiHost derived from the publishable key. The Request handed to the SDK is built from PATCHY_PUBLIC_BASE_URL with host and x-forwarded-* pinned from it. Only Set-Cookie and a handshake Location are forwarded from Clerk; x-clerk-auth-* and the Access-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_KEY and CLERK_SECRET_KEY required at startup; PATCHY_PUBLIC_BASE_URL loses its default; CLERK_JWT_KEY optional, validated with createPublicKey at boot; CLERK_AUTHORIZED_PARTIES optional, one origin. The server refuses to start without the required four, naming them.
  • RequireSession middleware in Auth providing Viewer (user, company, role, from Users.findByClerkId with the claim refresh): no session answers 401 with the door page; a session with no user row answers 303 to /join with the return path; a deactivated user gets the deactivated page (403, no-store, with a Sign out control). The Origin check on every first-party POST (403 on a foreign or missing Origin).
  • The shell moves from Serving's render.ts to core (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-src and connect-src for 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 with redirect_url = the validated return, default /join until Your machines exists); /join GET 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's Domain and Path, 303 to /login; needs a Clerk session and nothing else). /join for a person already in a company sends them to their return or /join's own "you are in <Company>" line until /company exists.
  • @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 reads PATCHY_DEV_CLERK_USER_ID from dev.env and seeds it as the admin's Clerk id; CLERK_JWT_KEY survives dev.env quoted.
  • Tests: Session against the keypair for signed-in, signed-out, handshake, both key shapes and the pk_live_ shape, with zero network; /login, /join and /logout in 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_KEY as test-facing, PATCHY_DEV_CLERK_USER_ID, what a local sign-in lands on), the patchy-dev-loop skill'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 dev instance with dev.env keys: /join signed out answers 401 with the door; after a Google sign-in with PATCHY_DEV_CLERK_USER_ID set, /join says the developer is in Patchy Dev; without it, /join creates a company and the next /join says so.
  • Not you? Sign out on /join lands on /login and the next /join is the door.
  • pnpm test stays 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 lint and 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.

Blocked by

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:corearea:docsarea:serverready-for-agent

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions