Sign in with your Ghosty account in previews
Updated 2026-11-22
Whoever opens a PR preview from Ghosty (owner, workspace member or accepted guest) is already signed in. Ghosty signs that identity and the preview proxy passes it to your app in the X-Ghosty-Identity header, like Cloudflare Access (Cf-Access-Jwt-Assertion), oauth2-proxy or Tailscale do. Your app verifies it and creates its session: a PR based on main can test the dashboard logged in, with no preview login of its own and no OAuth callback to register.
What arrives
-
X-Ghosty-Identity: a 12-hourEdDSAJWT withsub(Ghosty id),email,name,role(owner,memberorguest),iss=https://www.ghosty.studioandaud= that preview's host. It is useless in another preview or in production. -
The proxy strips any
X-Ghosty-Identitysent by the browser: only Ghosty's reaches the app. -
Every Ghosty link carries the identity: «Open preview» on a task or PR, the stable link
/c/dev/pr/<owner>/<repo>/<n>/preview, the box card and the signed links (/preview/s/…,/pv/…) when the opener has a Ghosty session. The proxy keeps the identity in a preview cookie (12 h), so navigating inside the app keeps it too. The public/pv/…link opened without an account carries no identity. -
In every preview:
GS_PREVIEW_IDENTITY_JWKS_URL(public key) andGS_PREVIEW_IDENTITY_ISSUER. They do not exist in production, so the recipe does nothing there. -
The smoke comes in the same way: the «Ghosty · Smoke» check arrives with a service identity (
sub: "ghosty-smoke",email: "smoke@ghosty.studio",role: "member"). If your app creates its session from this header, the smoke exercises your main feature signed in; you can treat thatsubas a test user.
The complete recipe (once, on main)
Four pieces that go together: with one missing, preview login gets fixed piecemeal and breaks the piece next to it (Formmy found out over three rounds: /login, then the button, then Google, and logout stopped working along the way).
1. Verify the identity. Only in Ghosty previews (the variable does not exist in production):
2. Sign in automatically. In the loader that reads the session (the one for /dashboard and the one for /login): with no session and previewIdentity(request) returning someone, sign in as a test user seeded in the preview database (find or create it with a test email such as preview-${role}@ghosty.test, keep sub and email only as data) and create the session like your normal login. If your session cookie carries a production Domain, drop it in the preview (isGhostyPreview()) or the browser will not store it.
3. Sign out and back in. Logout clears the session and sets the mark; the "Sign in with Ghosty" button clears it and signs in with the identity even if the mark exists:
4. No identity, a clean screen. Whoever opens the preview outside Ghosty (public link, another browser) sees no "Oops" or 500 and is not asked for credentials:
Real Google (or other OAuth), optional. If you uploaded to the preview the keys of a separate OAuth client for previews only, the Google button goes to Google with two changes: redirect_uri = process.env.GS_PREVIEW_OAUTH_REDIRECT_URI (when requesting the code and when exchanging it) and state = previewOAuthState(csrf, "/auth/google/callback") (signed with GS_PREVIEW_OAUTH_STATE_KEY; the 8-line helper is in ). Render both buttons, "Sign in with Ghosty" and "Sign in with Google", and if the keys are missing disable Google with the reason. Never edit the production client or copy its secret.
The preview database must be disposable (a copy or an empty seeded one), never production: testing with a session writes data. ghosty-dev doctor warns if the preview carries DATABASE_URL, MONGO_* or another database variable.
From an agent or the terminal
The link signs in as smoke@ghosty.studio (the smoke identity) and carries no visible keys. Creating it does not spend it: it is spent the first time it is opened (--open opens it in your browser).
Check it end to end
Declare in the .ghosty/factory.md frontmatter the protected route and how the session is closed and reopened:
ghosty-dev preview <pr> --login --check (and ghosty-dev doctor) makes the real requests, following redirects like a browser, and reports each step:
- enter: with identity the route ends in 2xx and without it ends elsewhere (
/dashboard → /dashboard/agents (200)vs→ / (200)counts as signed in). - sign out: the
logoutaction does not crash. - stays out: with the header still arriving, the route no longer signs in (the mark is respected).
- sign back in:
reloginreopens the session. - no identity:
reloginwithout identity returns the "Open it from Ghosty" screen, not a 500.
Without logout/relogin only step 1 runs and the rest shows "untested". Flags work too: --logout "POST /api/login intent=logout" --relogin "…".
In the evidence
"Tested in the preview of PR #N" only counts if #N contains the same commit being merged; ghosty-dev evidence and the gate check it.
The evidence and the PR template carry the required line "Flujo principal probado con sesión: " (main flow tested with a session) with a screenshot or a $ … command and its output: opening the page or a /login smoke does not count. The ghosty-dev/evidencia gate requires it.