fix(api): send a signed-out browser to sign in from a connect link
Fixes SUPER-3480: Connect on the Ahrefs plugin shows {"error":"Unauthorized"}.
Root cause
The problem is in our connect route, not in Ahrefs or OAuth. The desktop's Continue to Ahrefs (useConnector.openOAuth) opens GET /api/connectors/ahrefs_mcp/connect?organizationId=…&method=oauth2&plugin=ahrefs in the system browser. When that browser has no Paradev session cookie for the API, requireOrgMember returns a bare 401 {"error":"Unauthorized"} and the flow stops before discovery, registration or the authorize step.
The route is shared by every OAuth connector. The agent proxy's connect links (resolve-target.ts) and superset plugins connect open it the same way. Ahrefs is just the first new plugin a customer tried from a signed-out browser. The older integrations don't hit this because the desktop opens their web page (/integrations/linear), and that page signs the browser in first.
Fix
When requireOrgMember finds no session, it now redirects to <web>/sign-in?redirect=<connect URL> instead of returning 401. <web> is the NEXT_PUBLIC_WEB_URL twin on the request's own root domain (review follow-up), because the session cookie is scoped to one root domain and the API keeps api.superset.sh while the web moves to paradev.com. The sign-in page already accepts redirects back to the API origin (resolveCallbackUrl with the API in its trusted origins, plus better-auth trustedOrigins). So after signing in, the browser comes back to the connect route and goes on to the provider.
The fix is in the API so that released desktop and CLI builds get it without a new release. Every caller of requireOrgMember is a GET route reached by browser navigation; none expects a JSON 401.
Verification
Desktop dev app, driven over CDP, with the system browser on the same display:
- Before: Plugins → Ahrefs → Install → Continue to Ahrefs. The system browser shows
{"error":"Unauthorized"}and the API log showsGET /api/connectors/ahrefs_mcp/connect… 401. - After: same steps, browser signed out. The connect route 302s to sign-in, sign-in returns to the route, which 302s to
app.ahrefs.com/web/oauth/authorizewith our DCR client and a signed state. Ahrefs shows its Cloudflare check, then its sign-in page. - Unit test
requireOrgMember.test.tsfails on the old code and passes now.apps/api: 261 tests pass,tsc --noEmitand biome pass.
Not verified: the Ahrefs steps after its sign-in (consent, token exchange, tool call). They need a real Ahrefs Lite+ account, which I don't have. Ahrefs' DCR (/mcp/register) accepts our registration body as-is.
Known limitations / follow-ups
- On hosts outside our own domains (for example
*.vercel.apppreviews), web and API can't share a cookie. A browser already signed in on the web then lands on/instead of finishing the connect. It is not a loop. - A browser signed in to an account that is not in the org still gets a raw JSON
403from the same route. That's the same kind of dead end, but out of scope here (raised in review).
Unrelated observation
- The Connect dialog briefly titles itself "Connect ahrefs_mcp" (the slug) while the connector loads.
Evidence
Paradev page with repro steps, root cause, before/after screenshots and a video of the fix: https://app.superset.sh/page/super-3480-ahrefs-connect-shows-unauthorized-vawdbt
Summary by CodeRabbit
- Bug Fixes
- Unauthenticated requests to OAuth connector URLs now redirect to the sign-in page instead of returning an error response.
- The redirect preserves the original connector URL so users can return to it after signing in.
- The sign-in page uses a web domain matching the request when available, with the configured web URL as a fallback.