Skip to content

Commit

Permalink
fix(clerk-js): Reload env and retry if authenticateWithRedirect fails (
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosdouvlis authored Oct 14, 2024
1 parent 62f2617 commit c231cc7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .changeset/itchy-birds-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
2 changes: 1 addition & 1 deletion packages/clerk-js/src/core/resources/Environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Environment extends BaseResource implements EnvironmentResource {
this.fromJSON(data);
}

fetch({ touch = false }: { touch: boolean }): Promise<Environment> {
fetch({ touch }: { touch: boolean } = { touch: false }): Promise<Environment> {
if (touch) {
return this._basePatch({});
}
Expand Down
26 changes: 17 additions & 9 deletions packages/clerk-js/src/core/resources/SignUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,23 @@ export class SignUp extends BaseResource implements SignUpResource {
}: AuthenticateWithRedirectParams & {
unsafeMetadata?: SignUpUnsafeMetadata;
}): Promise<void> => {
const authenticateFn = (args: SignUpCreateParams | SignUpUpdateParams) =>
continueSignUp && this.id ? this.update(args) : this.create(args);

const { verifications } = await authenticateFn({
strategy,
redirectUrl: SignUp.clerk.buildUrlWithAuth(redirectUrl),
actionCompleteRedirectUrl: redirectUrlComplete,
unsafeMetadata,
emailAddress,
const authenticateFn = () => {
const params = {
strategy,
redirectUrl: SignUp.clerk.buildUrlWithAuth(redirectUrl),
actionCompleteRedirectUrl: redirectUrlComplete,
unsafeMetadata,
emailAddress,
};
return continueSignUp && this.id ? this.update(params) : this.create(params);
};

const { verifications } = await authenticateFn().catch(async () => {
// If captcha verification failed because the environment has changed, we need
// to reload the environment and try again one more time with the new environment.
// If this fails again, we will let the caller handle the error accordingly.
await SignUp.clerk.__unstable__environment!.reload();
return authenticateFn();
});

const { externalAccount } = verifications;
Expand Down

0 comments on commit c231cc7

Please sign in to comment.