Repository navigation
fix(client): confirm magic links with the code from the link fragment - #253
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 answers200on 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/clientclient.confirmMagicLink(token, code?)sends{ code }. Without a code it still sends{}.MagicLinkConfirmationgainssameDevice?: boolean, read from the409 confirmation_requiredbody. It is left out when the API does not send it.confirmMagicLinkSignIn({ client, token, code })passes the code.parseMagicLinkUrl(url)readstokenfrom the query andcodefrom the fragment. It takes a full URL (web, universal link or custom scheme) or a{ search, hash }location. It parses by hand, becauseURLis only partly implemented on some React Native runtimes. Acodein the query is ignored.takeMagicLinkParams()reads both fromwindow.location, then removes only those two values withhistory.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 toverified,confirmation-requiredorfailed. On a409withsameDevice === trueit confirms with the code at once. A200from an older API isverified.Verify screens (react, vue, svelte, angular)
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, thenfinishMagicLinkSignIn.history.replaceStatecall can log Kit's warning about bypassing its router. It is accepted because stripping the code early matters more, anddropQuery()then syncs Kit.react-native
There is no bundled verify screen or link parser in this package. It now re-exports
parseMagicLinkUrl,verifyMagicLinkSignIn,confirmMagicLinkSignIn,getMagicLinkConfirmationandmagicLinkConfirmationPrompt, with their types. The README says to read the incoming URL fromLinking.getInitialURL()and theurlevent, which keep the fragment for both universal and custom-scheme links, and not from route parameters such as expo-router'suseLocalSearchParams, which only carry the query.Tests
200from GET is verified with no confirm call, and a409withoutsameDevicestill prompts.400from confirm reports a failure (client flow and each screen).Checks
npm run typecheck,npm run lint,npm run format:checkandnpm test(945 Jest and 59 Vitest tests) pass.Changeset
minorfor client, react, vue, svelte, angular and react-native.Overlap with #252
#252 also edits the vue, svelte and angular
screens.test.tsfiles. 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 thefakeAdapterimport 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.tsandapp/verify-magiclink.tsxbuild their own verify flow. They need to read#code, confirm with it, and confirm without asking onsameDevice, before the API upgrades.confirmMagicLinkSignIn({ client, token })and treat only a200from verify as success. They break once the API upgrades. The React README example now shows the new flow.delivery.tokenmust usedelivery.magicLinkUrl(API side, noted in fix(magic-link): pin the landing page and bind confirm to a fragment code seamless-auth-api#614).