Skip to content

Commit 2e35f22

Browse files
Add support for handling fetch errors while recovery registration options fetch
1 parent efff4bc commit 2e35f22

File tree

3 files changed

+41
-29
lines changed

3 files changed

+41
-29
lines changed

lib/ts/recipe/webauthn/components/themes/recoverAccountWithToken/index.tsx

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,36 @@ function PasskeyRecoverAccountWithTokenTheme(props: RecoverAccountWithTokenTheme
6363
return;
6464
}
6565

66-
const registerOptions = await props.recipeImplementation.getRegisterOptions({
67-
userContext: props.userContext,
68-
recoverAccountToken: props.token,
69-
});
70-
if (registerOptions.status !== "OK") {
71-
switch (registerOptions.status) {
72-
case "RECOVER_ACCOUNT_TOKEN_INVALID_ERROR":
73-
setErrorMessageLabel("WEBAUTHN_ACCOUNT_RECOVERY_TOKEN_INVALID_ERROR");
74-
break;
75-
case "INVALID_EMAIL_ERROR":
76-
setErrorMessageLabel("WEBAUTHN_ACCOUNT_RECOVERY_INVALID_EMAIL_ERROR");
77-
break;
78-
case "INVALID_GENERATED_OPTIONS_ERROR":
79-
setErrorMessageLabel("WEBAUTHN_ACCOUNT_RECOVERY_INVALID_GENERATED_OPTIONS_ERROR");
80-
// TODO: Should we trigger an automatic retry here or will there
81-
// be a separate expired token related error?
82-
break;
83-
default:
84-
throw new Error("Should never come here");
66+
try {
67+
const registerOptions = await props.recipeImplementation.getRegisterOptions({
68+
userContext: props.userContext,
69+
recoverAccountToken: props.token,
70+
});
71+
if (registerOptions.status !== "OK") {
72+
switch (registerOptions.status) {
73+
case "RECOVER_ACCOUNT_TOKEN_INVALID_ERROR":
74+
setErrorMessageLabel("WEBAUTHN_ACCOUNT_RECOVERY_TOKEN_INVALID_ERROR");
75+
break;
76+
case "INVALID_EMAIL_ERROR":
77+
setErrorMessageLabel("WEBAUTHN_ACCOUNT_RECOVERY_INVALID_EMAIL_ERROR");
78+
break;
79+
case "INVALID_GENERATED_OPTIONS_ERROR":
80+
setErrorMessageLabel("WEBAUTHN_ACCOUNT_RECOVERY_INVALID_GENERATED_OPTIONS_ERROR");
81+
// TODO: Should we trigger an automatic retry here or will there
82+
// be a separate expired token related error?
83+
break;
84+
default:
85+
throw new Error("Should never come here");
86+
}
87+
88+
return;
8589
}
8690

87-
return;
91+
setRegisterOptions(registerOptions);
92+
} catch (err: any) {
93+
// This will likely be a fetch error.
94+
setErrorMessageLabel("WEBAUTHN_ACCOUNT_RECOVERY_FETCH_ERROR");
8895
}
89-
90-
setRegisterOptions(registerOptions);
9196
}, [props]);
9297

9398
useEffect(() => {
@@ -135,6 +140,7 @@ function PasskeyRecoverAccountWithTokenTheme(props: RecoverAccountWithTokenTheme
135140
onContinueClick={onContinueClick}
136141
errorMessageLabel={errorMessageLabel}
137142
email={registerOptions?.user.name || null}
143+
isContinueDisabled={registerOptions === null}
138144
/>
139145
{activeScreen !== RecoverAccountScreen.Success && (
140146
<AuthPageFooter
@@ -159,16 +165,18 @@ const RecoverAccountThemeInner = (
159165
onContinueClick: () => void;
160166
errorMessageLabel: string | null;
161167
email: string | null;
168+
isContinueDisabled: boolean;
162169
}
163170
) => {
164171
return props.activeScreen === RecoverAccountScreen.ContinueWithPasskey ? (
165172
<PasskeyConfirmation
166173
{...props}
167-
email={props.email || ""}
174+
email={props.email || undefined}
168175
onContinueClick={props.onContinueClick}
169176
errorMessageLabel={props.errorMessageLabel || undefined}
170177
isLoading={false}
171178
hideContinueWithoutPasskey
179+
isContinueDisabled={props.isContinueDisabled}
172180
/>
173181
) : props.activeScreen === RecoverAccountScreen.Success ? (
174182
<PasskeyRecoverAccountSuccess />

lib/ts/recipe/webauthn/components/themes/signUp/confirmation.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,31 @@ export const PasskeyConfirmation = withOverride(
2727
"PasskeyConfirmation",
2828
function PasskeyConfirmation(
2929
props: SignUpFormProps & {
30-
email: string;
30+
email?: string;
3131
onContinueClick: () => void;
3232
errorMessageLabel?: string;
3333
isLoading: boolean;
3434
hideContinueWithoutPasskey?: boolean;
35+
isContinueDisabled?: boolean;
3536
}
3637
): JSX.Element {
3738
const t = useTranslation();
3839

3940
return (
4041
<div data-supertokens="passkeyConfirmationContainer">
41-
<div data-supertokens="passkeyConfirmationEmailContainer">
42-
<div data-supertokens="continueWithLabel">{t("WEBAUTHN_CONTINUE_WITH_EMAIL_SUBTEXT")}</div>
43-
<div data-supertokens="enteredEmailId">{props.email}</div>
44-
</div>
42+
{props.email !== undefined && (
43+
<div data-supertokens="passkeyConfirmationEmailContainer">
44+
<div data-supertokens="continueWithLabel">{t("WEBAUTHN_CONTINUE_WITH_EMAIL_SUBTEXT")}</div>
45+
<div data-supertokens="enteredEmailId">{props.email}</div>
46+
</div>
47+
)}
4548
<PasskeyFeatureBlocks />
4649
{props.errorMessageLabel !== undefined && props.errorMessageLabel !== "" && (
4750
<RecoverableError errorMessageLabel={props.errorMessageLabel} />
4851
)}
4952
<div data-supertokens="passkeyConfirmationFooter">
5053
<Button
51-
disabled={false}
54+
disabled={props.isContinueDisabled}
5255
isLoading={props.isLoading}
5356
type="button"
5457
onClick={props.onContinueClick}

lib/ts/recipe/webauthn/components/themes/translations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ export const defaultTranslationsWebauthn = {
3737
WEBAUTHN_ACCOUNT_RECOVERY_INVALID_EMAIL_ERROR:
3838
"The email used is invalid. Please try with a different email ID or reach out to support.",
3939
WEBAUTHN_ACCOUNT_RECOVERY_INVALID_GENERATED_OPTIONS_ERROR: "Failed to recover account, please try again.",
40+
WEBAUTHN_ACCOUNT_RECOVERY_FETCH_ERROR: "Something went wrong, please refresh the page or reach out to support.",
4041
},
4142
};

0 commit comments

Comments
 (0)