Skip to content

fix(client): confirm magic links with the code from the link fragment - #253

Merged
Bccorb merged 1 commit into
mainfrom
fix/magic-link-fragment-code
Nov 21, 2026
Merged

Bccorb merged 1 commit into
mainfrom
fix/magic-link-fragment-code

Conversation

@Bccorb

@Bccorb Bccorb commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Client side of fells-code/seamless-auth-api#594 (API change: fells-code/seamless-auth-api#614). Part of fells-code/seamless-auth-api#603, step 6.

Release order

This is a client-first release. It must be published before the API release that carries fells-code/seamless-auth-api#614. Once that API is live, a client without this change can no longer complete a magic link sign-in: confirm without a code answers 400, and a same-device verify no longer signs in. This change keeps working against the current API, which ignores the confirm body and still answers 200 on a same-device verify.

What changes

The link becomes <target>?token=T#code=C. The token finds the link; the code, which never reaches a server, a Referer or an access log, proves the reader holds the whole link.

@seamless-auth/client

  • client.confirmMagicLink(token, code?) sends { code }. Without a code it still sends {}.
  • MagicLinkConfirmation gains sameDevice?: boolean, read from the 409 confirmation_required body. It is left out when the API does not send it.
  • confirmMagicLinkSignIn({ client, token, code }) passes the code.
  • New flows:
    • parseMagicLinkUrl(url) reads token from the query and code from the fragment. It takes a full URL (web, universal link or custom scheme) or a { search, hash } location. It parses by hand, because URL is only partly implemented on some React Native runtimes. A code in the query is ignored.
    • takeMagicLinkParams() reads both from window.location, then removes only those two values with history.replaceState, keeping other query and fragment values and the history state. Outside a browser it returns nulls.
    • verifyMagicLinkSignIn({ client, token, code }) verifies the link and resolves to verified, confirmation-required or failed. On a 409 with sameDevice === true it confirms with the code at once. A 200 from an older API is verified.

Verify screens (react, vue, svelte, angular)

  • Each screen calls takeMagicLinkParams() as it is created (first render in React, setup in Vue, component init in Svelte, construction in Angular), so the code leaves the address bar before effects or later scripts read it. The router's own query and fragment are used first where the router may not be the window (memory routers in tests and embedded apps).
  • sameDevice === true: confirm without prompting, then finishMagicLinkSignIn.
  • Otherwise the existing prompt shows, and approving confirms with the code.
  • A refused code shows "Failed to verify token". The API spends the link on a wrong code, so the screen does not offer a retry on that path.
  • The existing router-based query drop stays, which also brings each router in line with the cleaned address. In SvelteKit dev, the direct history.replaceState call can log Kit's warning about bypassing its router. It is accepted because stripping the code early matters more, and dropQuery() then syncs Kit.

react-native

There is no bundled verify screen or link parser in this package. It now re-exports parseMagicLinkUrl, verifyMagicLinkSignIn, confirmMagicLinkSignIn, getMagicLinkConfirmation and magicLinkConfirmationPrompt, with their types. The README says to read the incoming URL from Linking.getInitialURL() and the url event, which keep the fragment for both universal and custom-scheme links, and not from route parameters such as expo-router's useLocalSearchParams, which only carry the query.

Tests

  • New API, same device: confirmed with the fragment code and no prompt (client flow and each screen).
  • New API, cross device: prompted, then confirmed with the code (client flow and each screen).
  • Older API: 200 from GET is verified with no confirm call, and a 409 without sameDevice still prompts.
  • Wrong code: 400 from confirm reports a failure (client flow and each screen).
  • The token and code are stripped from the address (client helper, React, Vue, Angular, Svelte), with other query and fragment values kept.
  • URL parsing: web, custom scheme with other fragment values, code in the query ignored, undecodable values.

Checks

npm run typecheck, npm run lint, npm run format:check and npm test (945 Jest and 59 Vitest tests) pass.

Changeset

minor for client, react, vue, svelte, angular and react-native.

Overlap with #252

#252 also edits the vue, svelte and angular screens.test.ts files. Its angular and svelte hunks append at the end of the file and do not touch these lines. Its vue hunk changes the import block next to the fakeAdapter import this PR extends, so whichever merges second may need a one-line import rebase. No source files overlap.

Callers outside this repo

  • roxtarget-mobile: src/auth/magicLink.ts and app/verify-magiclink.tsx build their own verify flow. They need to read #code, confirm with it, and confirm without asking on sameDevice, before the API upgrades.
  • Custom landing pages written from the old README call confirmMagicLinkSignIn({ client, token }) and treat only a 200 from verify as success. They break once the API upgrades. The React README example now shows the new flow.
  • External delivery callers that build a URL from delivery.token must use delivery.magicLinkUrl (API side, noted in fix(magic-link): pin the landing page and bind confirm to a fragment code seamless-auth-api#614).

Read token from the query and code from the fragment, strip both from the
address bar as the verify screen starts, and send the code on confirm.
Confirm without prompting when the API reports sameDevice. Older APIs
ignore the code and still answer 200 on a same-device verify.

Refs fells-code/seamless-auth-api#594
@Bccorb
Bccorb merged commit c6d36c7 into main Nov 21, 2026
5 checks passed
@Bccorb
Bccorb deleted the fix/magic-link-fragment-code branch November 21, 2026 04:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant