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
5 changes: 5 additions & 0 deletions .changeset/blue-cloths-create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': minor
---

[Experimental] Add support for captcha to Signal SignUp
27 changes: 26 additions & 1 deletion packages/clerk-js/src/core/resources/SignUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
AuthenticateWithPopupParams,
AuthenticateWithRedirectParams,
AuthenticateWithWeb3Params,
CaptchaWidgetType,
CreateEmailLinkFlowReturn,
PrepareEmailAddressVerificationParams,
PreparePhoneNumberVerificationParams,
Expand Down Expand Up @@ -487,11 +488,35 @@ class SignUpFuture implements SignUpFutureResource {
return this.resource.unverifiedFields;
}

private async getCaptchaToken(): Promise<{
captchaToken?: string;
captchaWidgetType?: CaptchaWidgetType;
captchaError?: unknown;
}> {
const captchaChallenge = new CaptchaChallenge(SignUp.clerk);
const response = await captchaChallenge.managedOrInvisible({ action: 'signup' });
if (!response) {
throw new Error('Captcha challenge failed');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this use ClerkRuntimeError? Or not because it's only internal?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is me kicking that can down the road until I can do an error pass across the entire implementation 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget 😉

}

const { captchaError, captchaToken, captchaWidgetType } = response;
return { captchaToken, captchaWidgetType, captchaError };
}

async password({ emailAddress, password }: { emailAddress: string; password: string }): Promise<{ error: unknown }> {
return runAsyncResourceTask(this.resource, async () => {
const { captchaToken, captchaWidgetType, captchaError } = await this.getCaptchaToken();

await this.resource.__internal_basePost({
path: this.resource.pathRoot,
body: { emailAddress, password },
body: {
strategy: 'password',
emailAddress,
password,
captchaToken,
captchaWidgetType,
captchaError,
},
});
});
}
Comment on lines 506 to 522
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Password flow unconditionally requires CAPTCHA; bypass and retry semantics missing

As written, password() always requests a CAPTCHA token and will throw if CAPTCHA is disabled/unavailable (e.g., bypass or dev env). This diverges from create() and authenticateWithRedirectOrPopup(), which gate CAPTCHA and retry on environment/captcha errors. This will break password signup in environments where CAPTCHA is intentionally bypassed or temporarily unavailable.

Proposed fix:

  • Gate CAPTCHA retrieval on the same conditions used elsewhere (respect build flag and client bypass).
  • Retry once on CAPTCHA-related API errors after reloading the environment.
  • Spread optional captchaParams into the request body to omit absent fields.

Apply this diff:

   async password({ emailAddress, password }: { emailAddress: string; password: string }): Promise<{ error: unknown }> {
     return runAsyncResourceTask(this.resource, async () => {
-      const { captchaToken, captchaWidgetType, captchaError } = await this.getCaptchaToken();
-
-      await this.resource.__internal_basePost({
-        path: this.resource.pathRoot,
-        body: {
-          strategy: 'password',
-          emailAddress,
-          password,
-          captchaToken,
-          captchaWidgetType,
-          captchaError,
-        },
-      });
+      const shouldFetchCaptcha = !__BUILD_DISABLE_RHC__ && !SignUp.clerk.client?.captchaBypass;
+
+      let captchaParams: Partial<{
+        captchaToken: string;
+        captchaWidgetType: CaptchaWidgetType;
+        captchaError: unknown;
+      }> = {};
+
+      if (shouldFetchCaptcha) {
+        captchaParams = await this.getCaptchaToken();
+      }
+
+      const post = () =>
+        this.resource.__internal_basePost({
+          path: this.resource.pathRoot,
+          body: {
+            strategy: 'password',
+            emailAddress,
+            password,
+            ...captchaParams,
+          },
+        });
+
+      try {
+        await post();
+      } catch (e) {
+        if (isClerkAPIResponseError(e) && isCaptchaError(e)) {
+          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+          await SignUp.clerk.__unstable__environment!.reload();
+          if (shouldFetchCaptcha) {
+            captchaParams = await this.getCaptchaToken();
+          }
+          await post();
+        } else {
+          throw e;
+        }
+      }
     });
   }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async password({ emailAddress, password }: { emailAddress: string; password: string }): Promise<{ error: unknown }> {
return runAsyncResourceTask(this.resource, async () => {
const { captchaToken, captchaWidgetType, captchaError } = await this.getCaptchaToken();
await this.resource.__internal_basePost({
path: this.resource.pathRoot,
body: { emailAddress, password },
body: {
strategy: 'password',
emailAddress,
password,
captchaToken,
captchaWidgetType,
captchaError,
},
});
});
}
async password({ emailAddress, password }: { emailAddress: string; password: string }): Promise<{ error: unknown }> {
return runAsyncResourceTask(this.resource, async () => {
const shouldFetchCaptcha = !__BUILD_DISABLE_RHC__ && !SignUp.clerk.client?.captchaBypass;
let captchaParams: Partial<{
captchaToken: string;
captchaWidgetType: CaptchaWidgetType;
captchaError: unknown;
}> = {};
if (shouldFetchCaptcha) {
captchaParams = await this.getCaptchaToken();
}
const post = () =>
this.resource.__internal_basePost({
path: this.resource.pathRoot,
body: {
strategy: 'password',
emailAddress,
password,
...captchaParams,
},
});
try {
await post();
} catch (e) {
if (isClerkAPIResponseError(e) && isCaptchaError(e)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await SignUp.clerk.__unstable__environment!.reload();
if (shouldFetchCaptcha) {
captchaParams = await this.getCaptchaToken();
}
await post();
} else {
throw e;
}
}
});
}
🤖 Prompt for AI Agents
In packages/clerk-js/src/core/resources/SignUp.ts around lines 506-522, the
password() flow always requests a CAPTCHA token which fails in bypass/dev
environments; update it to only call getCaptchaToken when the same
CAPTCHA-enabled conditions used by create() and
authenticateWithRedirectOrPopup() are met (respecting build flags and client
bypass), spread the optional captchaParams into the POST body so absent fields
aren’t sent, and wrap the API call in a retry that, on CAPTCHA-related API
errors, reloads the environment and retries the POST once before failing.

Expand Down
9 changes: 8 additions & 1 deletion packages/clerk-js/src/utils/captcha/CaptchaChallenge.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { CaptchaWidgetType } from '@clerk/types';

import type { Clerk } from '../../core/resources/internal';
import { getCaptchaToken } from './getCaptchaToken';
import { retrieveCaptchaInfo } from './retrieveCaptchaInfo';
Expand Down Expand Up @@ -42,7 +44,12 @@ export class CaptchaChallenge {
*
* Managed challenged start as non-interactive and escalate to interactive if necessary.
*/
public async managedOrInvisible(opts?: Partial<CaptchaOptions>) {
public async managedOrInvisible(
opts?: Partial<CaptchaOptions>,
): Promise<
| { captchaError?: string; captchaAction?: string; captchaToken?: string; captchaWidgetType?: CaptchaWidgetType }
| undefined
> {
const { captchaSiteKey, canUseCaptcha, captchaWidgetType, captchaProvider, captchaPublicKeyInvisible, nonce } =
retrieveCaptchaInfo(this.clerk);

Expand Down
Loading