|
| 1 | +import { AgreeToTermsPolicy, SignInIdentifier } from '@logto/schemas'; |
| 2 | +import { useCallback, useEffect, useState } from 'react'; |
| 3 | +import { useParams } from 'react-router-dom'; |
| 4 | + |
| 5 | +import { |
| 6 | + identifyAndSubmitInteraction, |
| 7 | + signInWithVerifiedIdentifier, |
| 8 | + verifyOneTimeToken, |
| 9 | +} from '@/apis/experience'; |
| 10 | +import LoadingLayer from '@/components/LoadingLayer'; |
| 11 | +import useApi from '@/hooks/use-api'; |
| 12 | +import useErrorHandler from '@/hooks/use-error-handler'; |
| 13 | +import useFallbackRoute from '@/hooks/use-fallback-route'; |
| 14 | +import useGlobalRedirectTo from '@/hooks/use-global-redirect-to'; |
| 15 | +import useLoginHint from '@/hooks/use-login-hint'; |
| 16 | +import usePreSignInErrorHandler from '@/hooks/use-pre-sign-in-error-handler'; |
| 17 | +import useTerms from '@/hooks/use-terms'; |
| 18 | + |
| 19 | +import ErrorPage from '../ErrorPage'; |
| 20 | +import SwitchAccount from '../SwitchAccount'; |
| 21 | + |
| 22 | +const OneTimeToken = () => { |
| 23 | + const { token } = useParams(); |
| 24 | + const fallback = useFallbackRoute(); |
| 25 | + const email = useLoginHint(); |
| 26 | + const [mismatchedAccount, setMismatchedAccount] = useState<string>(); |
| 27 | + const [oneTimeTokenError, setOneTimeTokenError] = useState<unknown>(); |
| 28 | + |
| 29 | + const asyncRegisterWithOneTimeToken = useApi(identifyAndSubmitInteraction); |
| 30 | + const asyncSignInWithVerifiedIdentifier = useApi(signInWithVerifiedIdentifier); |
| 31 | + const asyncVerifyOneTimeToken = useApi(verifyOneTimeToken); |
| 32 | + |
| 33 | + const { termsValidation, agreeToTermsPolicy } = useTerms(); |
| 34 | + const handleError = useErrorHandler(); |
| 35 | + const redirectTo = useGlobalRedirectTo(); |
| 36 | + const preSignInErrorHandler = usePreSignInErrorHandler(); |
| 37 | + |
| 38 | + const signInWithOneTimeToken = useCallback( |
| 39 | + async (verificationId: string) => { |
| 40 | + const [error, result] = await asyncSignInWithVerifiedIdentifier(verificationId); |
| 41 | + |
| 42 | + if (error) { |
| 43 | + await handleError(error, preSignInErrorHandler); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if (result?.redirectTo) { |
| 48 | + await redirectTo(result.redirectTo); |
| 49 | + } |
| 50 | + }, |
| 51 | + [preSignInErrorHandler, asyncSignInWithVerifiedIdentifier, handleError, redirectTo] |
| 52 | + ); |
| 53 | + |
| 54 | + const registerWithOneTimeToken = useCallback( |
| 55 | + async (verificationId: string) => { |
| 56 | + const [error, result] = await asyncRegisterWithOneTimeToken({ verificationId }); |
| 57 | + |
| 58 | + if (error) { |
| 59 | + await handleError(error, { |
| 60 | + 'user.email_already_in_use': async () => { |
| 61 | + await signInWithOneTimeToken(verificationId); |
| 62 | + }, |
| 63 | + }); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (result?.redirectTo) { |
| 68 | + await redirectTo(result.redirectTo); |
| 69 | + } |
| 70 | + }, |
| 71 | + [asyncRegisterWithOneTimeToken, handleError, redirectTo, signInWithOneTimeToken] |
| 72 | + ); |
| 73 | + |
| 74 | + useEffect(() => { |
| 75 | + (async () => { |
| 76 | + if (token && email) { |
| 77 | + /** |
| 78 | + * Check if the user has agreed to the terms and privacy policy before navigating to the 3rd-party social sign-in page |
| 79 | + * when the policy is set to `Manual` |
| 80 | + */ |
| 81 | + if (agreeToTermsPolicy === AgreeToTermsPolicy.Manual && !(await termsValidation())) { |
| 82 | + return; |
| 83 | + } |
| 84 | + const [error, result] = await asyncVerifyOneTimeToken({ |
| 85 | + token, |
| 86 | + identifier: { type: SignInIdentifier.Email, value: email }, |
| 87 | + }); |
| 88 | + |
| 89 | + if (error) { |
| 90 | + await handleError(error, { |
| 91 | + 'one_time_token.email_mismatch': () => { |
| 92 | + setMismatchedAccount(email); |
| 93 | + }, |
| 94 | + 'one_time_token.token_expired': () => { |
| 95 | + setOneTimeTokenError(error); |
| 96 | + }, |
| 97 | + 'one_time_token.token_consumed': () => { |
| 98 | + setOneTimeTokenError(error); |
| 99 | + }, |
| 100 | + 'one_time_token.token_revoked': () => { |
| 101 | + setOneTimeTokenError(error); |
| 102 | + }, |
| 103 | + 'one_time_token.token_not_found': () => { |
| 104 | + setOneTimeTokenError(error); |
| 105 | + }, |
| 106 | + }); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + if (!result?.verificationId) { |
| 111 | + return; |
| 112 | + } |
| 113 | + await registerWithOneTimeToken(result.verificationId); |
| 114 | + } |
| 115 | + |
| 116 | + window.location.replace('/' + fallback); |
| 117 | + })(); |
| 118 | + }, [ |
| 119 | + agreeToTermsPolicy, |
| 120 | + email, |
| 121 | + fallback, |
| 122 | + token, |
| 123 | + asyncVerifyOneTimeToken, |
| 124 | + handleError, |
| 125 | + registerWithOneTimeToken, |
| 126 | + termsValidation, |
| 127 | + ]); |
| 128 | + |
| 129 | + if (mismatchedAccount) { |
| 130 | + return <SwitchAccount account={mismatchedAccount} />; |
| 131 | + } |
| 132 | + |
| 133 | + if (oneTimeTokenError) { |
| 134 | + return <ErrorPage title="error.invalid_link" message="error.invalid_link_description" />; |
| 135 | + } |
| 136 | + |
| 137 | + return <LoadingLayer />; |
| 138 | +}; |
| 139 | +export default OneTimeToken; |
0 commit comments