-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(experience): add one-time token landing page to handle magic link auth #7159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
charIeszhao
merged 3 commits into
master
from
charles-log-10876-experience-build-landing-page-to-handle-token-parameter
Mar 25, 2025
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { InteractionEvent, type OneTimeTokenVerificationVerifyPayload } from '@logto/schemas'; | ||
|
|
||
| import api from '../api'; | ||
|
|
||
| import { experienceApiRoutes, type VerificationResponse } from './const'; | ||
| import { initInteraction } from './interaction'; | ||
|
|
||
| export const registerWithOneTimeToken = async (payload: OneTimeTokenVerificationVerifyPayload) => { | ||
| await initInteraction(InteractionEvent.Register); | ||
|
|
||
| return api | ||
| .post(`${experienceApiRoutes.verification}/one-time-token/verify`, { | ||
| json: payload, | ||
| }) | ||
| .json<VerificationResponse>(); | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { experience } from '@logto/schemas'; | ||
| import { useMemo } from 'react'; | ||
|
|
||
| const useFallbackRoute = () => | ||
| useMemo(() => { | ||
| const fallbackKey = new URLSearchParams(window.location.search).get('fallback'); | ||
| return ( | ||
| Object.entries(experience.routes).find(([key]) => key === fallbackKey)?.[1] ?? | ||
| experience.routes.signIn | ||
| ); | ||
| }, []); | ||
|
|
||
| export default useFallbackRoute; | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| import { | ||
| AgreeToTermsPolicy, | ||
| InteractionEvent, | ||
| SignInIdentifier, | ||
| type RequestErrorBody, | ||
| } from '@logto/schemas'; | ||
| import { condString } from '@silverhand/essentials'; | ||
| import { useCallback, useEffect, useState } from 'react'; | ||
| import { useParams } from 'react-router-dom'; | ||
|
|
||
| import { | ||
| identifyAndSubmitInteraction, | ||
| signInWithVerifiedIdentifier, | ||
| registerWithOneTimeToken, | ||
| } from '@/apis/experience'; | ||
| import LoadingLayer from '@/components/LoadingLayer'; | ||
| import useApi from '@/hooks/use-api'; | ||
| import useErrorHandler from '@/hooks/use-error-handler'; | ||
| import useGlobalRedirectTo from '@/hooks/use-global-redirect-to'; | ||
| import useLoginHint from '@/hooks/use-login-hint'; | ||
| import usePreSignInErrorHandler from '@/hooks/use-pre-sign-in-error-handler'; | ||
| import useTerms from '@/hooks/use-terms'; | ||
|
|
||
| import ErrorPage from '../ErrorPage'; | ||
|
|
||
| const OneTimeToken = () => { | ||
| const { token } = useParams(); | ||
| const email = useLoginHint(); | ||
| const [oneTimeTokenError, setOneTimeTokenError] = useState<RequestErrorBody | boolean>(); | ||
|
|
||
| const asyncIdentifyUserAndSubmit = useApi(identifyAndSubmitInteraction); | ||
| const asyncSignInWithVerifiedIdentifier = useApi(signInWithVerifiedIdentifier); | ||
| const asyncRegisterWithOneTimeToken = useApi(registerWithOneTimeToken); | ||
|
|
||
| const { termsValidation, agreeToTermsPolicy } = useTerms(); | ||
| const handleError = useErrorHandler(); | ||
| const redirectTo = useGlobalRedirectTo(); | ||
| const preSignInErrorHandler = usePreSignInErrorHandler(); | ||
| const preRegisterErrorHandler = usePreSignInErrorHandler({ | ||
| interactionEvent: InteractionEvent.Register, | ||
| }); | ||
|
|
||
| /** | ||
| * Update interaction event to `SignIn`, and then identify user and submit. | ||
| */ | ||
| const signInWithOneTimeToken = useCallback( | ||
| async (verificationId: string) => { | ||
| const [error, result] = await asyncSignInWithVerifiedIdentifier(verificationId); | ||
|
|
||
| if (error) { | ||
| await handleError(error, preSignInErrorHandler); | ||
| return; | ||
| } | ||
|
|
||
| if (result?.redirectTo) { | ||
| await redirectTo(result.redirectTo); | ||
| } | ||
| }, | ||
| [preSignInErrorHandler, asyncSignInWithVerifiedIdentifier, handleError, redirectTo] | ||
| ); | ||
|
|
||
| /** | ||
| * Always try to submit the one-time token interaction with `Register` event first. | ||
| * If the email already exists, call the `signInWithOneTimeToken` function instead. | ||
| */ | ||
| const submit = useCallback( | ||
| async (verificationId: string) => { | ||
| const [error, result] = await asyncIdentifyUserAndSubmit({ verificationId }); | ||
|
|
||
| if (error) { | ||
| await handleError(error, { | ||
| 'user.email_already_in_use': async () => { | ||
| await signInWithOneTimeToken(verificationId); | ||
| }, | ||
| ...preRegisterErrorHandler, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| if (result?.redirectTo) { | ||
| await redirectTo(result.redirectTo); | ||
| } | ||
| }, | ||
| [ | ||
| preRegisterErrorHandler, | ||
| asyncIdentifyUserAndSubmit, | ||
| handleError, | ||
| redirectTo, | ||
| signInWithOneTimeToken, | ||
| ] | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| (async () => { | ||
| if (!token || !email) { | ||
| setOneTimeTokenError(true); | ||
| return; | ||
| } | ||
|
|
||
| /** | ||
| * Check if the user has agreed to the terms and privacy policy before navigating to the 3rd-party social sign-in page | ||
| * when the policy is set to `Manual` | ||
| */ | ||
| if (agreeToTermsPolicy === AgreeToTermsPolicy.Manual && !(await termsValidation())) { | ||
| return; | ||
| } | ||
|
|
||
| const [error, result] = await asyncRegisterWithOneTimeToken({ | ||
| token, | ||
| identifier: { type: SignInIdentifier.Email, value: email }, | ||
| }); | ||
|
|
||
| if (error) { | ||
| await handleError(error, { | ||
| global: (error: RequestErrorBody) => { | ||
| setOneTimeTokenError(error); | ||
| }, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| if (!result?.verificationId) { | ||
| return; | ||
| } | ||
|
|
||
| await submit(result.verificationId); | ||
| })(); | ||
| }, [ | ||
| agreeToTermsPolicy, | ||
| email, | ||
| token, | ||
| asyncRegisterWithOneTimeToken, | ||
| handleError, | ||
| termsValidation, | ||
| submit, | ||
| ]); | ||
|
|
||
| if (oneTimeTokenError) { | ||
| return ( | ||
| <ErrorPage | ||
| title="error.invalid_link" | ||
| message="error.invalid_link_description" | ||
| rawMessage={condString(typeof oneTimeTokenError !== 'boolean' && oneTimeTokenError.message)} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| return <LoadingLayer />; | ||
| }; | ||
| export default OneTimeToken; |
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
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
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.