-
-
Notifications
You must be signed in to change notification settings - Fork 512
/
Copy pathindex.tsx
148 lines (129 loc) · 4.31 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {
AgreeToTermsPolicy,
ExtraParamsKey,
InteractionEvent,
SignInIdentifier,
type RequestErrorBody,
} from '@logto/schemas';
import { condString } from '@silverhand/essentials';
import { useCallback, useEffect, useState } from 'react';
import { useSearchParams } 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 useSubmitInteractionErrorHandler from '@/hooks/use-submit-interaction-error-handler';
import useTerms from '@/hooks/use-terms';
import ErrorPage from '../ErrorPage';
const OneTimeToken = () => {
const [params] = useSearchParams();
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 = useSubmitInteractionErrorHandler(InteractionEvent.SignIn);
const preRegisterErrorHandler = useSubmitInteractionErrorHandler(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 () => {
const token = params.get(ExtraParamsKey.OneTimeToken);
const email = params.get(ExtraParamsKey.LoginHint);
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,
params,
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;