fix: offer to switch account when a link is for another organization, keep JSON for API clients
Closed
KI
19m ago+597+597+012345678901234567890123456789-68-68-01234567890123456789
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:
- 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 userequireOrgMember. The Slack link route had the same dead end, as plain text. - API clients. #9819 made
requireOrgMemberredirect every request without a session, so a JSON client got a 302 to an HTML page instead of a 401. - Other browser links. Document file links (
/api/{projects,tasks}/…/files/…) showed a bareUnauthorized. The sign-in page explained an expired session only for connector links.
What
apps/api/src/lib/browserAuth.tsgeneralizes #9819'ssignInUrl.signedOut(request, apiResponse)andwrongAccount(request, apiResponse)redirect a browser navigation and returnapiResponseto anything else. A browser navigation hastext/htmlin Accept (the same parser as the domain move, now exported asacceptsHtml) and noAuthorization,x-api-keyorx-trpc-sourceheader. The web page is still picked on the request's own root domain, andreason=session_expiredbehaves as before.requireOrgMember,/api/integrations/slack/linkand the two file routes use these helpers.- New
/switch-accountpage (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'sconnectSignInContext. 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 indomain-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.
- The return path goes through the existing
- Sign-in page. A non-connector link with
reason=session_expirednow shows "Your Paradev session in this browser has expired. Sign in again to continue." instead of "Sign in to continue to Paradev". resolveCallbackUrlandconnectSignInContextmoved toapps/web/src/app/utils/, because the sign-in page and/switch-accountboth 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 account | API client |
|---|---|---|---|---|
GET /api/connectors/<slug>/connect | sign-in (#9819) | sign-in + expired copy (#9823) | was 403 JSON → /switch-account | was 302 → 401/403 JSON again |
GET /api/integrations/{linear,google,slack,notion,sentry,microsoft-teams}/connect, GET /api/github/install | sign-in (#9819) | sign-in; expired copy now shown | was 403 JSON → /switch-account | was 302 → 401/403 JSON again |
GET /api/integrations/slack/link | sign-in (now on the request's root domain) | same, with expired copy | was 403 text → /switch-account | 401 / 403 text |
GET /api/{projects,tasks}/<id>/files/<file> | was 401 text → sign-in | was 401 text → sign-in + expired copy | 404 (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=… | same | membership re-checked; ?error=unauthorized on the web page | n/a |
GET /api/github/user/callback | HTML page (state carries identity) | same | same | n/a |
GET /api/auth/desktop/connect | starts sign-in (no session needed) | same | n/a | n/a |
GET /auth/integration-connect (web, legacy) | sign-in, then continues | same | continues to the API connect route, which now shows /switch-account | n/a |
/oauth/consent, /cli/auth/code, web pages (/connect/<slug>, /integrations/*, /plugins/*) | proxy → sign-in with return path | same | Uses the active org, so no org id in the link to mismatch. Consent lets the user pick | n/a |
/accept-invitation/<id> | The token-based accept works without a session | same | the invitation's own error message | n/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/web127 pass,packages/shareddomain-move tests pass.bun run typecheckexit 0,bun run lint:fixclean, andbun run check:i18npasses.- 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/authorizewith our client and signed state. The flow continues. curlchecks: a JSON client gets401 {"error":"Unauthorized"}. A browser Accept plusAuthorizationalso 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/htmlin Accept, or withAuthorization,x-api-key, orx-trpc-source) instead of a 302 redirect. - Applies to all
requireOrgMemberconnect routes, the Slack link route, and the project/task document file links, which were showing bareUnauthorizedtext. - The sign-in page now explains that the Paradev session expired for non-connector links as well as connector links.
/switch-accountstays 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 withredirectvalidated byresolveCallbackUrl.resolveCallbackUrlandconnectSignInContextmoved from the sign-in route into sharedapps/web/src/app/utils/.