Title: Sign-in token redemption is unusable in an in-app browser: `session_exists`, then `sign_in_token_already_used` · Issue #9451 · clerk/javascript · GitHub
Open Graph Title: Sign-in token redemption is unusable in an in-app browser: `session_exists`, then `sign_in_token_already_used` · Issue #9451 · clerk/javascript
X Title: Sign-in token redemption is unusable in an in-app browser: `session_exists`, then `sign_in_token_already_used` · Issue #9451 · clerk/javascript
Description: Preliminary Checks I have reviewed the documentation: https://clerk.com/docs I have searched for existing issues: https://github.com/clerk/javascript/issues I have not already reached out to Clerk support via email or Discord (if you hav...
Open Graph Description: Preliminary Checks I have reviewed the documentation: https://clerk.com/docs I have searched for existing issues: https://github.com/clerk/javascript/issues I have not already reached out to Clerk ...
X Description: Preliminary Checks I have reviewed the documentation: https://clerk.com/docs I have searched for existing issues: https://github.com/clerk/javascript/issues I have not already reached out to Clerk ...
Opengraph URL: https://github.com/clerk/javascript/issues/9451
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Sign-in token redemption is unusable in an in-app browser: `session_exists`, then `sign_in_token_already_used`","articleBody":"### Preliminary Checks\n\n- [x] I have reviewed the documentation: https://clerk.com/docs\n\n- [x] I have searched for existing issues: https://github.com/clerk/javascript/issues\n\n- [x] I have not already reached out to Clerk support via email or Discord (if you have, no need to open an issue here)\n\n- [x] This issue is not a question, general help request, or anything other than a bug report directly related to Clerk. Please ask questions in our Discord community: https://clerk.com/discord.\n\n\n### Reproduction\n\nhttps://github.com/scoobydrew83/worthsync\n\n### Publishable key\n\npk_live_Y2xlcmsud29ydGhzeW5jLmNvbSQ\n\n### Description\n\n## Summary\n\nWe implemented Clerk's documented native→web session handoff (mint a sign-in token on the\nbackend, open a URL in the app's in-app browser, redeem with `signIn.ticket()`). It cannot be\nmade to work as documented. Two distinct defects compound, and the fix for the second one is a\nPR from a Clerk engineer that was closed without merging.\n\nWe have a working workaround. We are filing this because the workaround depends on undocumented\nbehaviour that could change under us, and because the documented path is broken for anyone who\ntries it.\n\n## Environment\n\n| | |\n|---|---|\n| `@clerk/nextjs` | 7.5.1 (also reported by others on 7.6.3 and 7.7.4) |\n| `clerk-js` | 6.x, served from our custom FAPI domain |\n| `@clerk/expo` | 4.2.3 |\n| Next.js | 16, App Router, deployed on Cloudflare Workers via OpenNext |\n| Instance | **Production**, custom FAPI domain, **single session mode** |\n| Native | Expo SDK 57, `expo-web-browser` → SFSafariViewController (iOS) / Custom Tabs (Android) |\n\n## What we're trying to do\n\nThe mobile app signs the user in natively with `@clerk/expo`. Several screens are web-only\n(reports, household sharing/invites, policy documents, account deletion), and the app opens them\nin an in-app browser. We want those to open **already signed in**.\n\nPer the docs, we:\n\n1. `POST /v1/sign_in_tokens` with `{ user_id, expires_in_seconds: 60 }`\n2. Open `https://app.example.com/sign-in-with-token?token=\u003cTOKEN>&redirect_url=/settings/privacy`\n3. On that page, call `signIn.ticket({ ticket })` then `signIn.finalize({ navigate })`\n\nAccount deletion in particular is an **App Store Review Guideline 5.1.1(v)** requirement, so this\nis not a nice-to-have — a reviewer meeting a sign-in wall is a rejection.\n\n---\n\n## Defect 1 — `session_exists` makes the documented example a silent no-op\n\nThe in-app browser usually already holds a Clerk session (on iOS, one our own app created during\na previous handoff — SFSafariViewController has a persistent per-app store). In single-session\nmode, `signIn.ticket()` then fails:\n\n```\nsession_exists — \"Session already exists\"\n\"You're currently in single session mode. You can only be signed into one account at a time.\"\n```\n\n**The official example does not handle this.** From\n`docs/guides/development/custom-flows/authentication/embedded-email-links.mdx`:\n\n```tsx\nif (!signInToken || user || loading) {\n return // \u003c-- silently does nothing when a session exists\n}\n```\n\nSo the documented flow's behaviour in our scenario is \"quietly do nothing,\" which is\nindistinguishable from a bug in the caller.\n\n**`setActive({ session: null })` does not clear it.** Reading `clerk-js` (`core/clerk.ts`),\n`#touchCurrentSession` early-returns when the session is null and `client.sessions` is never\nmutated — it issues no FAPI request at all. The Frontend API still sees the session. This is not\nobvious from the API surface, and \"set the active session to nothing\" is exactly what a reader\nwould reach for.\n\nOnly `client.removeSessions()` / `signOut()` actually issue `DELETE /v1/client/sessions`.\n\n**Prior report:** [clerk/javascript#8044](https://github.com/clerk/javascript/issues/8044) —\nsame defect, filed in March of this year, **closed by a staleness bot with zero maintainer\nreplies**, and re-reported by two more users on 2026-07-30 against `@clerk/nextjs@7.6.3`. It is\nnot fixed in any released version.\n\n---\n\n## Defect 2 — `signOut()` triggers a Safari hard reload that respends the single-use token\n\nWorking around Defect 1 by signing out first produces the next failure:\n\n```\nsign_in_token_already_used_code — \"Sign in token has already been used.\"\n```\n\n**This reproduces on iOS and not on Android, with identical code.**\n\nThe mechanism is documented in Clerk's own\n[PR #7873](https://github.com/clerk/javascript/pull/7873) by `manovotny`:\n\n1. `clerk-js` calls `onBeforeSetActive()` during sign-out **deliberately without** the\n `'sign-out'` intent (there is a comment explaining why).\n2. `@clerk/nextjs` skips its `invalidateCacheAction()` server action **only when that intent is\n `'sign-out'`** — so for `signOut()` the action fires.\n3. `invalidateCacheAction()` calls `cookies().delete()` inside a server action, which in\n Next.js 15+ re-renders the current page's RSC tree as part of the response.\n4. **In Safari that RSC delivery fails (`TypeError: Load failed`), and Next.js falls back to a\n hard browser navigation.**\n5. The reload restores the URL **from Next's internal router state** — token included — and the\n page redeems the already-consumed token.\n\nThat PR proposes the fix (drop the `&& intent === 'sign-out'` condition). It is **closed and\nnever merged**; the condition is still present in `main` today.\n\nNote this also means `history.replaceState` is not a sufficient mitigation, because Next's\nrouter state is not updated by it — as the PR itself points out.\n\nPossibly related and still open:\n[clerk/javascript#9405](https://github.com/clerk/javascript/issues/9405) (`invalidateCacheAction`\nfiring on Next 16 + `@clerk/nextjs` 7.5.x–7.7.x, different symptom, same machinery).\n\n---\n\n## What we shipped, in case it's useful to others\n\n```tsx\n// 1. Guard must survive a full page RELOAD — not a re-render, not a remount.\n// A useRef or module-scope flag is reborn in the new document.\nconst attemptKey = `ticket_attempt:${token.slice(-24)}`;\nconst alreadyAttempted = sessionStorage.getItem(attemptKey) !== null;\nsessionStorage.setItem(attemptKey, \"1\");\n\n// 2. If a previous document already spent it, don't redeem — recover.\nif (alreadyAttempted) {\n const live = clerk.client?.signedInSessions?.[0];\n if (live) { await clerk.setActive({ session: live.id, navigate: ... }); return; }\n}\n\n// 3. Skip redemption entirely when the browser already holds the right user.\nconst existing = clerk.client?.signedInSessions ?? [];\nconst mine = existing.find(s => s.user?.id === expectedUserId);\nif (mine) { await clerk.setActive({ session: mine.id, navigate: ... }); return; }\n\n// 4. Otherwise clear server-side. removeSessions(), NOT signOut() — signOut()\n// routes through onBeforeSetActive and triggers Defect 2.\nif (existing.length > 0) await clerk.client.removeSessions();\n\nconst { error } = await signIn.ticket({ ticket: token });\n```\n\nTwo things worth calling out for the docs:\n\n- `clerk.client.signedInSessions` is the only reliable signal here.\n `useAuth().isSignedIn` is seeded from server-rendered state and reads `false` until `clerk-js`\n has fetched `/v1/client`, so guarding on it means the clear never runs.\n- After a successful `POST /v1/client/sign_ins` the session is attached to\n `clerk.client.signedInSessions`, but `clerk.session` stays `null` until `setActive` runs — so\n `if (clerk.session)` is the wrong recovery check.\n\n---\n\n## What we're asking for\n\n1. **Ship PR #7873, or an equivalent fix.** Right now `signOut()` on Safari + Next.js 15/16 can\n trigger a hard reload that replays the current URL. That is a general hazard, not specific to\n ticket flows — any page holding one-time state in its query string is exposed.\n\n2. **Reopen or comment on #8044.** It is a real, reproducible defect that was closed by a bot\n without a maintainer ever looking at it, and it has since been re-reported against 7.6.3.\n\n3. **Fix the `accept-token` docs example.** As written it silently no-ops when a session already\n exists. It should show the existing-session path — that is the *common* case in any embedded\n browser, not an edge case.\n\n4. **Is there a supported way to do native→webview session handoff?** We're aware of\n [clerk-docs#2483](https://github.com/clerk/clerk-docs/issues/2483) (\"Clerk does not support\n Webviews environments\") and\n [clerk/javascript#3880](https://github.com/clerk/javascript/issues/3880). We also found a\n closed, unmerged `clerk-ios` PR\n ([#357](https://github.com/clerk/clerk-ios/pull/357)) adding `prepareAuthenticatedWebURL()`\n backed by `POST /v1/client/prepare_webview`, which is exactly this use case.\n\n **Is `/v1/client/prepare_webview` live on production FAPI?** If so we would use it and delete\n all of the above. If not, is a supported handoff on the roadmap?\n\n5. **Would you consider a `signIn.ticket()` option that replaces an existing session** rather\n than erroring? Every consumer of this API in an embedded browser has to hand-roll the\n sign-out dance, and getting it wrong burns a single-use token.\n\n## Reproduction\n\nAny production Clerk instance in single-session mode, Next.js 15/16 App Router:\n\n1. Sign in normally in Safari so a session exists.\n2. Mint a sign-in token via `POST /v1/sign_in_tokens`.\n3. Visit `/your-accept-page?token=\u003cTOKEN>` in the same browser.\n4. Call `signIn.ticket({ ticket })` → `session_exists`.\n5. Call `await signOut()` first, then `signIn.ticket({ ticket })` → in Safari,\n `sign_in_token_already_used_code`, because the page reloaded and redeemed twice.\n\nAndroid/Chrome does not reproduce step 5.\n\n### Environment\n\n```Shell\n| | |\n|---|---|\n| `@clerk/nextjs` | 7.5.1 (also reported by others on 7.6.3 and 7.7.4) |\n| `clerk-js` | 6.x, served from our custom FAPI domain |\n| `@clerk/expo` | 4.2.3 |\n| Next.js | 16, App Router, deployed on Cloudflare Workers via OpenNext |\n| Instance | **Production**, custom FAPI domain, **single session mode** |\n| Native | Expo SDK 57, `expo-web-browser` → SFSafariViewController (iOS) / Custom Tabs (Android) |\n```","author":{"url":"https://github.com/scoobydrew83","@type":"Person","name":"scoobydrew83"},"datePublished":"2026-08-14T01:45:54Z","url":"https://github.com/clerk/javascript/issues/9451"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:037f96f2-0fc0-baf0-87f6-efbd54f94c9d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AD28:1A169A:73E5DB:A356D5:6AFB68E1 |
| html-safe-nonce | b3c10b20d3e617d3f76277882d9ef62d14c92511d3ffa9e03c302b93a354dd42 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRDI4OjFBMTY5QTo3M0U1REI6QTM1NkQ1OjZBRkI2OEUxIiwidmlzaXRvcl9pZCI6IjkwMzM2NTA1Mzg0Nzc3NDIzMDUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5bb02e875275d71aac5c60b192ba29b12cbd309c9935883b57ea14bdda1d250c |
| hovercard-subject-tag | issue:5147386396 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/clerk/javascript/9451/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7d9ba0cbe1117d448c26369584d986029f9fdbc9e42804b7c1a2387821583659/clerk/javascript/issues/9451 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7d9ba0cbe1117d448c26369584d986029f9fdbc9e42804b7c1a2387821583659/clerk/javascript/issues/9451 |
| og:image:alt | Preliminary Checks I have reviewed the documentation: https://clerk.com/docs I have searched for existing issues: https://github.com/clerk/javascript/issues I have not already reached out to Clerk ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | scoobydrew83 |
| hostname | github.com |
| expected-hostname | github.com |
| None | ea33d13c96d0ff66bd8fa906b193d2213536b78cb06b0c50a4632c58b1418f94 |
| turbo-cache-control | no-preview |
| go-import | github.com/clerk/javascript git https://github.com/clerk/javascript.git |
| octolytics-dimension-user_id | 49538330 |
| octolytics-dimension-user_login | clerk |
| octolytics-dimension-repository_id | 438331492 |
| octolytics-dimension-repository_nwo | clerk/javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 438331492 |
| octolytics-dimension-repository_network_root_nwo | clerk/javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 59ef97164c52cf59bd10558ddec9c4f482763988 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width