Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8a38d05

Browse files
committedMar 24, 2025··
refactor(experience): update username registration handler
update username registraction handler, directly submit the interaction is password is not enabled for sign-up
1 parent f34984b commit 8a38d05

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed
 

‎packages/experience/src/components/IdentifierRegisterForm/use-register-with-username.ts

+43-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
import { useState, useCallback, useMemo } from 'react';
22
import { useNavigate } from 'react-router-dom';
33

4-
import { registerWithUsername } from '@/apis/experience';
4+
import { identifyAndSubmitInteraction, registerWithUsername } from '@/apis/experience';
55
import useApi from '@/hooks/use-api';
66
import type { ErrorHandlers } from '@/hooks/use-error-handler';
77
import useErrorHandler from '@/hooks/use-error-handler';
8+
import useGlobalRedirectTo from '@/hooks/use-global-redirect-to';
9+
import usePreSignInErrorHandler from '@/hooks/use-pre-sign-in-error-handler';
10+
import { useSieMethods } from '@/hooks/use-sie';
811

912
const useRegisterWithUsername = () => {
1013
const navigate = useNavigate();
14+
const redirectTo = useGlobalRedirectTo();
15+
1116
const [errorMessage, setErrorMessage] = useState<string>();
1217

18+
const { passwordRequiredForSignUp } = useSieMethods();
19+
1320
const clearErrorMessage = useCallback(() => {
1421
setErrorMessage('');
1522
}, []);
1623

17-
const errorHandlers: ErrorHandlers = useMemo(
24+
const usernameErrorHandlers: ErrorHandlers = useMemo(
1825
() => ({
1926
'user.username_already_in_use': (error) => {
2027
setErrorMessage(error.message);
@@ -23,21 +30,52 @@ const useRegisterWithUsername = () => {
2330
[]
2431
);
2532

33+
const preSignInErrorHandler = usePreSignInErrorHandler({ replace: true });
34+
2635
const handleError = useErrorHandler();
2736
const asyncRegister = useApi(registerWithUsername);
2837

38+
const asyncSubmitInteraction = useApi(identifyAndSubmitInteraction);
39+
40+
const onSubmitInteraction = useCallback(async () => {
41+
const [error, result] = await asyncSubmitInteraction();
42+
43+
if (error) {
44+
await handleError(error, preSignInErrorHandler);
45+
return;
46+
}
47+
48+
if (result) {
49+
await redirectTo(result.redirectTo);
50+
}
51+
}, [asyncSubmitInteraction, handleError, preSignInErrorHandler, redirectTo]);
52+
2953
const onSubmit = useCallback(
3054
async (username: string) => {
3155
const [error] = await asyncRegister(username);
3256

3357
if (error) {
34-
await handleError(error, errorHandlers);
58+
await handleError(error, usernameErrorHandlers);
59+
return;
60+
}
61+
62+
// If password is required for sign up, navigate to the password page
63+
if (passwordRequiredForSignUp) {
64+
navigate('password');
3565
return;
3666
}
3767

38-
navigate('password');
68+
// Otherwise, identify and submit interaction
69+
await onSubmitInteraction();
3970
},
40-
[asyncRegister, errorHandlers, handleError, navigate]
71+
[
72+
asyncRegister,
73+
passwordRequiredForSignUp,
74+
onSubmitInteraction,
75+
handleError,
76+
usernameErrorHandlers,
77+
navigate,
78+
]
4179
);
4280

4381
return { errorMessage, clearErrorMessage, onSubmit };

‎packages/experience/src/hooks/use-sie.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type UseSieMethodsReturnType = {
2525
* @see {useRequiredProfileErrorHandler} for more information.
2626
*/
2727
signUpMethods: SignInIdentifier[];
28+
passwordRequiredForSignUp: boolean;
2829
signInMethods: SignInExperienceResponse['signIn']['methods'];
2930
socialSignInSettings: SignInExperienceResponse['socialSignIn'];
3031
socialConnectors: SignInExperienceResponse['socialConnectors'];
@@ -102,6 +103,11 @@ export const useSieMethods = (): UseSieMethodsReturnType => {
102103
[experienceSettings?.signInMode, signInMethods]
103104
);
104105

106+
const passwordRequiredForSignUp = useMemo(() => {
107+
const { signUp } = experienceSettings ?? {};
108+
return signUp?.password ?? false;
109+
}, [experienceSettings]);
110+
105111
return useMemo(
106112
() => ({
107113
signUpMethods,
@@ -113,13 +119,21 @@ export const useSieMethods = (): UseSieMethodsReturnType => {
113119
forgotPassword: experienceSettings?.forgotPassword,
114120
customContent: experienceSettings?.customContent,
115121
singleSignOnEnabled: experienceSettings?.singleSignOnEnabled,
122+
passwordRequiredForSignUp,
116123
isVerificationCodeEnabledForSignUp,
117124
isVerificationCodeEnabledForSignIn,
118125
}),
119126
[
120127
signUpMethods,
121128
signInMethods,
122-
experienceSettings,
129+
experienceSettings?.socialSignIn,
130+
experienceSettings?.socialConnectors,
131+
experienceSettings?.ssoConnectors,
132+
experienceSettings?.signInMode,
133+
experienceSettings?.forgotPassword,
134+
experienceSettings?.customContent,
135+
experienceSettings?.singleSignOnEnabled,
136+
passwordRequiredForSignUp,
123137
isVerificationCodeEnabledForSignUp,
124138
isVerificationCodeEnabledForSignIn,
125139
]

‎packages/experience/src/pages/RegisterPassword/index.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import useApi from '@/hooks/use-api';
99
import { usePromiseConfirmModal } from '@/hooks/use-confirm-modal';
1010
import useErrorHandler, { type ErrorHandlers } from '@/hooks/use-error-handler';
1111
import useGlobalRedirectTo from '@/hooks/use-global-redirect-to';
12-
import useMfaErrorHandler from '@/hooks/use-mfa-error-handler';
1312
import usePasswordPolicyChecker from '@/hooks/use-password-policy-checker';
1413
import usePasswordRejectionErrorHandler from '@/hooks/use-password-rejection-handler';
14+
import usePreSignInErrorHandler from '@/hooks/use-pre-sign-in-error-handler';
1515
import { usePasswordPolicy, useSieMethods } from '@/hooks/use-sie';
1616

1717
import ErrorPage from '../ErrorPage';
@@ -31,7 +31,7 @@ const RegisterPassword = () => {
3131
const asyncRegisterPassword = useApi(continueRegisterWithPassword);
3232
const handleError = useErrorHandler();
3333

34-
const mfaErrorHandler = useMfaErrorHandler({ replace: true });
34+
const preSignInErrorHandler = usePreSignInErrorHandler({ replace: true });
3535
const passwordRejectionErrorHandler = usePasswordRejectionErrorHandler({ setErrorMessage });
3636

3737
const errorHandlers: ErrorHandlers = useMemo(
@@ -41,10 +41,10 @@ const RegisterPassword = () => {
4141
await show({ type: 'alert', ModalContent: error.message, cancelText: 'action.got_it' });
4242
navigate(-1);
4343
},
44-
...mfaErrorHandler,
44+
...preSignInErrorHandler,
4545
...passwordRejectionErrorHandler,
4646
}),
47-
[mfaErrorHandler, passwordRejectionErrorHandler, show, navigate]
47+
[preSignInErrorHandler, passwordRejectionErrorHandler, show, navigate]
4848
);
4949

5050
const onSubmitHandler = useCallback(

0 commit comments

Comments
 (0)
Please sign in to comment.