fix: offer to switch account when a link is for another organization, keep JSON for API clients

Closed
KI
19m ago
+597-68

Follow-up to SUPER-3480. Stacked on #9823, which is stacked on #9819. This PR targets #9823's branch, so the diff shows only this change.

Why

#9819 and #9823 send a signed-out browser from a connect link to sign in. Three gaps were left:

  1. Wrong account. A browser signed in to an account that is not in the link's organization still got a raw {"error":"User is not a member of this organization"} 403. This applies to all 8 connect routes that use requireOrgMember. The Slack link route had the same dead end, as plain text.
  2. API clients. #9819 made requireOrgMember redirect every request without a session, so a JSON client got a 302 to an HTML page instead of a 401.
  3. Other browser links. Document file links (/api/{projects,tasks}/…/files/…) showed a bare Unauthorized. The sign-in page explained an expired session only for connector links.

What

  • apps/api/src/lib/browserAuth.ts generalizes #9819's signInUrl. signedOut(request, apiResponse) and wrongAccount(request, apiResponse) redirect a browser navigation and return apiResponse to anything else. A browser navigation has text/html in Accept (the same parser as the domain move, now exported as acceptsHtml) and no Authorization, x-api-key or x-trpc-source header. The web page is still picked on the request's own root domain, and reason=session_expired behaves as before.
  • requireOrgMember, /api/integrations/slack/link and the two file routes use these helpers.
  • New /switch-account page (web). It says "You are signed in as {email}. This account is not a member of the organization this link is for." It names the connector when the link is a connector connect link, reusing #9823's connectSignInContext. Sign in with another account signs out and goes to /sign-in?redirect=<link>, so the flow continues after sign-in. A signed-out visitor goes straight to sign-in. The page does not show the organization's name, because the visitor is not a member.
    • The return path goes through the existing resolveCallbackUrl (web origin or the API and its own-domain twins), so it cannot be used for an open redirect.
    • It is a public route in proxy-routes, and it is retained on the legacy host in domain-move. It must sign out the session on the root domain the API saw, so a move to paradev.com would act on the wrong cookie.
  • Sign-in page. A non-connector link with reason=session_expired now shows "Your Paradev session in this browser has expired. Sign in again to continue." instead of "Sign in to continue to Paradev".
  • resolveCallbackUrl and connectSignInContext moved to apps/web/src/app/utils/, because the sign-in page and /switch-account both use them now. The files did not change.
  • 5 new strings, translated in all 17 locales.

Audit: browser-navigable routes

"API client" means no text/html Accept, or an Authorization/x-api-key/x-trpc-source header. "Before" is main + #9819 + #9823.

Route(a) no session(b) invalid / expired cookie(c) wrong accountAPI client
GET /api/connectors/<slug>/connectsign-in (#9819)sign-in + expired copy (#9823)was 403 JSON → /switch-accountwas 302 → 401/403 JSON again
GET /api/integrations/{linear,google,slack,notion,sentry,microsoft-teams}/connect, GET /api/github/installsign-in (#9819)sign-in; expired copy now shownwas 403 JSON → /switch-accountwas 302 → 401/403 JSON again
GET /api/integrations/slack/linksign-in (now on the request's root domain)same, with expired copywas 403 text → /switch-account401 / 403 text
GET /api/{projects,tasks}/<id>/files/<file>was 401 text → sign-inwas 401 text → sign-in + expired copy404 (unchanged, so the file's existence is not revealed)401 text
Connector + legacy OAuth callbacks (/api/connectors/callback/*, /api/integrations/*/callback, /api/github/callback)Not session based. The state cookie + signed state are re-checked, and a failure redirects to the web page with ?error=…samemembership re-checked; ?error=unauthorized on the web pagen/a
GET /api/github/user/callbackHTML page (state carries identity)samesamen/a
GET /api/auth/desktop/connectstarts sign-in (no session needed)samen/an/a
GET /auth/integration-connect (web, legacy)sign-in, then continuessamecontinues to the API connect route, which now shows /switch-accountn/a
/oauth/consent, /cli/auth/code, web pages (/connect/<slug>, /integrations/*, /plugins/*)proxy → sign-in with return pathsameUses the active org, so no org id in the link to mismatch. Consent lets the user pickn/a
/accept-invitation/<id>The token-based accept works without a sessionsamethe invitation's own error messagen/a
GET /api (agent discovery)401 JSON with a WWW-Authenticate hint (left as is, for agents)

Not changed: a missing organizationId still returns 400 JSON. That is a malformed link, not a session problem.

Verification

  • requireOrgMember.test.ts: new tests for wrong account (browser → /switch-account), member passes, and API clients (no Accept, JSON Accept, Authorization, x-trpc-source) getting 401/403 JSON for both no session and wrong account. 5 of them fail on #9823's code (checked by restoring the old file) and pass now.
  • apps/api: all pass except 3 Slack event tests (webidl.util.markAsUncloneable). These fail the same way on #9823's branch without this change. apps/web 127 pass, packages/shared domain-move tests pass. bun run typecheck exit 0, bun run lint:fix clean, and bun run check:i18n passes.
  • End to end on the dev stack (api :8681, web :8680) in headless Chrome with a fresh profile, driven over CDP:
    • no cookie → Ahrefs connect link → "Sign in to connect Ahrefs"
    • invalid session cookie → Linear connect link → "Welcome back. Your Paradev session in this browser has expired. Sign in again to continue." (before: "Sign in to continue to Paradev")
    • signed in as a new user outside the org → Ahrefs connect link. Before: {"error":"User is not a member of this organization"}. After: "Switch account to connect Ahrefs. You are signed in as outsider-…@example.com…"
    • Sign in with another account → sign-in page for the same link → sign in as the org's admin → app.ahrefs.com/web/oauth/authorize with our client and signed state. The flow continues.
    • curl checks: a JSON client gets 401 {"error":"Unauthorized"}. A browser Accept plus Authorization also gets 401. Before this change, a JSON client got a 302.
  • Not tested on production domains. The root-domain choice and the domain-move retention are covered by unit tests only.

Summary by cubic

Connect links in a browser now lead to sign-in or, when the signed-in account isn't in the link's organization, a new "switch account" page; API clients keep their 401/403 JSON responses.

  • Warns a browser signed in to the wrong account to switch instead of showing a raw {"error":"User is not a member of this organization"} 403.
  • Returns 401/403 JSON to API clients (no text/html in Accept, or with Authorization, x-api-key, or x-trpc-source) instead of a 302 redirect.
  • Applies to all requireOrgMember connect routes, the Slack link route, and the project/task document file links, which were showing bare Unauthorized text.
  • The sign-in page now explains that the Paradev session expired for non-connector links as well as connector links.
  • /switch-account stays on the legacy host during the domain move because it signs out that host's session cookie, and it is added as a public proxy route with redirect validated by resolveCallbackUrl.
  • resolveCallbackUrl and connectSignInContext moved from the sign-in route into shared apps/web/src/app/utils/.