Skip to content

Commit

Permalink
fix(clerk-js): Only retry oauth if captcha failed (#4329)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosdouvlis authored Oct 14, 2024
1 parent 7cff9d8 commit d64e54c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/nasty-melons-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@clerk/clerk-js": patch
"@clerk/shared": patch
---

Only retry the OAuth flow if the captcha check failed.
10 changes: 7 additions & 3 deletions packages/clerk-js/src/core/resources/SignUp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Poller } from '@clerk/shared';
import { isCaptchaError, isClerkAPIResponseError } from '@clerk/shared/error';
import type {
AttemptEmailAddressVerificationParams,
AttemptPhoneNumberVerificationParams,
Expand Down Expand Up @@ -271,12 +272,15 @@ export class SignUp extends BaseResource implements SignUpResource {
return continueSignUp && this.id ? this.update(params) : this.create(params);
};

const { verifications } = await authenticateFn().catch(async () => {
const { verifications } = await authenticateFn().catch(async e => {
// 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();
if (isClerkAPIResponseError(e) && isCaptchaError(e)) {
await SignUp.clerk.__unstable__environment!.reload();
return authenticateFn();
}
throw e;
});

const { externalAccount } = verifications;
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export function isUnauthorizedError(e: any): boolean {
return code === 'authentication_invalid' && status === 401;
}

export function isCaptchaError(e: ClerkAPIResponseError): boolean {
return ['captcha_invalid', 'captcha_not_enabled', 'captcha_missing_token'].includes(e.errors[0].code);
}

export function is4xxError(e: any): boolean {
const status = e?.status;
return !!status && status >= 400 && status < 500;
Expand Down

0 comments on commit d64e54c

Please sign in to comment.