Skip to content

Commit e77d008

Browse files
nikosdouvliswobsoriano
authored andcommitted
fix(clerk-js): Reload env and retry if authenticateWithRedirect fails (#4327)
1 parent 310c7e4 commit e77d008

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

.changeset/itchy-birds-juggle.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

packages/clerk-js/src/core/resources/Environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class Environment extends BaseResource implements EnvironmentResource {
3333
this.fromJSON(data);
3434
}
3535

36-
fetch({ touch = false }: { touch: boolean }): Promise<Environment> {
36+
fetch({ touch }: { touch: boolean } = { touch: false }): Promise<Environment> {
3737
if (touch) {
3838
return this._basePatch({});
3939
}

packages/clerk-js/src/core/resources/SignUp.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,23 @@ export class SignUp extends BaseResource implements SignUpResource {
260260
}: AuthenticateWithRedirectParams & {
261261
unsafeMetadata?: SignUpUnsafeMetadata;
262262
}): Promise<void> => {
263-
const authenticateFn = (args: SignUpCreateParams | SignUpUpdateParams) =>
264-
continueSignUp && this.id ? this.update(args) : this.create(args);
265-
266-
const { verifications } = await authenticateFn({
267-
strategy,
268-
redirectUrl: SignUp.clerk.buildUrlWithAuth(redirectUrl),
269-
actionCompleteRedirectUrl: redirectUrlComplete,
270-
unsafeMetadata,
271-
emailAddress,
263+
const authenticateFn = () => {
264+
const params = {
265+
strategy,
266+
redirectUrl: SignUp.clerk.buildUrlWithAuth(redirectUrl),
267+
actionCompleteRedirectUrl: redirectUrlComplete,
268+
unsafeMetadata,
269+
emailAddress,
270+
};
271+
return continueSignUp && this.id ? this.update(params) : this.create(params);
272+
};
273+
274+
const { verifications } = await authenticateFn().catch(async () => {
275+
// If captcha verification failed because the environment has changed, we need
276+
// to reload the environment and try again one more time with the new environment.
277+
// If this fails again, we will let the caller handle the error accordingly.
278+
await SignUp.clerk.__unstable__environment!.reload();
279+
return authenticateFn();
272280
});
273281

274282
const { externalAccount } = verifications;

0 commit comments

Comments
 (0)