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/sixty-ways-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Pass the `action` property to Turnstile
2 changes: 1 addition & 1 deletion packages/clerk-js/src/core/auth/CaptchaHeartbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class CaptchaHeartbeat {
}

try {
const params = await this.captchaChallenge.invisible();
const params = await this.captchaChallenge.invisible({ action: 'heartbeat' });
await this.clerk.client.sendCaptchaToken(params);
} catch {
// Ignore unhandled errors
Expand Down
2 changes: 1 addition & 1 deletion packages/clerk-js/src/core/fraudProtection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ export class FraudProtection {
}

public managedChallenge(clerk: Clerk) {
return new this.CaptchaChallengeImpl(clerk).managedInModal();
return new this.CaptchaChallengeImpl(clerk).managedInModal({ action: 'verify' });
}
}
2 changes: 1 addition & 1 deletion packages/clerk-js/src/core/resources/SignUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class SignUp extends BaseResource implements SignUpResource {

if (!__BUILD_DISABLE_RHC__ && !this.clientBypass() && !this.shouldBypassCaptchaForAttempt(params)) {
const captchaChallenge = new CaptchaChallenge(SignUp.clerk);
const captchaParams = await captchaChallenge.managedOrInvisible();
const captchaParams = await captchaChallenge.managedOrInvisible({ action: 'signup' });
if (!captchaParams) {
throw new ClerkRuntimeError('', { code: 'captcha_unavailable' });
}
Expand Down
6 changes: 4 additions & 2 deletions packages/clerk-js/src/utils/captcha/CaptchaChallenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class CaptchaChallenge {
* This will always use the non-interactive variant of the CAPTCHA challenge and will
* always use the fallback key.
*/
public async invisible() {
public async invisible(opts?: Partial<CaptchaOptions>) {
const { captchaSiteKey, canUseCaptcha, captchaPublicKeyInvisible } = retrieveCaptchaInfo(this.clerk);

if (canUseCaptcha && captchaSiteKey && captchaPublicKeyInvisible) {
Expand All @@ -20,6 +20,7 @@ export class CaptchaChallenge {
invisibleSiteKey: captchaPublicKeyInvisible,
widgetType: 'invisible',
captchaProvider: 'turnstile',
action: opts?.action,
}).catch(e => {
if (e.captchaError) {
return { captchaError: e.captchaError };
Expand Down Expand Up @@ -65,12 +66,13 @@ export class CaptchaChallenge {
* Similar to managed() but will render the CAPTCHA challenge in a modal
* managed by clerk-js itself.
*/
public async managedInModal() {
public async managedInModal(opts?: Partial<CaptchaOptions>) {
return this.managedOrInvisible({
modalWrapperQuerySelector: '#cl-modal-captcha-wrapper',
modalContainerQuerySelector: '#cl-modal-captcha-container',
openModal: () => this.clerk.__internal_openBlankCaptchaModal(),
closeModal: () => this.clerk.__internal_closeBlankCaptchaModal(),
action: opts?.action,
});
}
}
7 changes: 7 additions & 0 deletions packages/clerk-js/src/utils/captcha/turnstile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ interface RenderOptions {
* @default 'always'
*/
appearance?: 'always' | 'execute' | 'interaction-only';
/**
* A custom value that can be used to differentiate widgets under the same sitekey
* in analytics and which is returned upon validation. This can only contain up to
* 32 alphanumeric characters including _ and -.
*/
action?: string;
}

interface Turnstile {
Expand Down Expand Up @@ -166,6 +172,7 @@ export const getTurnstileToken = async (opts: CaptchaOptions) => {
const id = captcha.render(widgetContainerQuerySelector, {
sitekey: turnstileSiteKey,
appearance: 'interaction-only',
action: opts.action,
retry: 'never',
'refresh-expired': 'auto',
callback: function (token: string) {
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/utils/captcha/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type CaptchaOptions = {
modalWrapperQuerySelector?: string;
openModal?: () => Promise<unknown>;
closeModal?: () => Promise<unknown>;
action?: 'verify' | 'signup' | 'heartbeat';
};

export type GetCaptchaTokenReturn = {
Expand Down