Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading