Skip to content

Commit 4ac7b25

Browse files
authored
feat(clerk-js): Add support for captcha to Signal SignUp (#6574)
1 parent 7bb644a commit 4ac7b25

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

.changeset/blue-cloths-create.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': minor
3+
---
4+
5+
[Experimental] Add support for captcha to Signal SignUp

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
AuthenticateWithPopupParams,
99
AuthenticateWithRedirectParams,
1010
AuthenticateWithWeb3Params,
11+
CaptchaWidgetType,
1112
CreateEmailLinkFlowReturn,
1213
PrepareEmailAddressVerificationParams,
1314
PreparePhoneNumberVerificationParams,
@@ -487,11 +488,35 @@ class SignUpFuture implements SignUpFutureResource {
487488
return this.resource.unverifiedFields;
488489
}
489490

491+
private async getCaptchaToken(): Promise<{
492+
captchaToken?: string;
493+
captchaWidgetType?: CaptchaWidgetType;
494+
captchaError?: unknown;
495+
}> {
496+
const captchaChallenge = new CaptchaChallenge(SignUp.clerk);
497+
const response = await captchaChallenge.managedOrInvisible({ action: 'signup' });
498+
if (!response) {
499+
throw new Error('Captcha challenge failed');
500+
}
501+
502+
const { captchaError, captchaToken, captchaWidgetType } = response;
503+
return { captchaToken, captchaWidgetType, captchaError };
504+
}
505+
490506
async password({ emailAddress, password }: { emailAddress: string; password: string }): Promise<{ error: unknown }> {
491507
return runAsyncResourceTask(this.resource, async () => {
508+
const { captchaToken, captchaWidgetType, captchaError } = await this.getCaptchaToken();
509+
492510
await this.resource.__internal_basePost({
493511
path: this.resource.pathRoot,
494-
body: { emailAddress, password },
512+
body: {
513+
strategy: 'password',
514+
emailAddress,
515+
password,
516+
captchaToken,
517+
captchaWidgetType,
518+
captchaError,
519+
},
495520
});
496521
});
497522
}

packages/clerk-js/src/utils/captcha/CaptchaChallenge.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { CaptchaWidgetType } from '@clerk/types';
2+
13
import type { Clerk } from '../../core/resources/internal';
24
import { getCaptchaToken } from './getCaptchaToken';
35
import { retrieveCaptchaInfo } from './retrieveCaptchaInfo';
@@ -42,7 +44,12 @@ export class CaptchaChallenge {
4244
*
4345
* Managed challenged start as non-interactive and escalate to interactive if necessary.
4446
*/
45-
public async managedOrInvisible(opts?: Partial<CaptchaOptions>) {
47+
public async managedOrInvisible(
48+
opts?: Partial<CaptchaOptions>,
49+
): Promise<
50+
| { captchaError?: string; captchaAction?: string; captchaToken?: string; captchaWidgetType?: CaptchaWidgetType }
51+
| undefined
52+
> {
4653
const { captchaSiteKey, canUseCaptcha, captchaWidgetType, captchaProvider, captchaPublicKeyInvisible, nonce } =
4754
retrieveCaptchaInfo(this.clerk);
4855

0 commit comments

Comments
 (0)