Skip to content

Commit

Permalink
fix(clerk-js): Fix Smart captcha on Sign Up ticket flow (#3351)
Browse files Browse the repository at this point in the history
* fix(clerk-js): Fix smart captcha on ticket flow

* fix(clerk-js): Fix lint
  • Loading branch information
anagstef authored May 9, 2024
1 parent bb6a13f commit 9968287
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-eggs-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Fix Smart CAPTCHA on ticket flow
2 changes: 2 additions & 0 deletions packages/clerk-js/src/ui/elements/LoadingCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PropsWithChildren } from 'react';

import { descriptors, Flex, Spinner } from '../customizables';
import { CaptchaElement } from './CaptchaElement';
import { Card } from './Card';
import { useCardState, withCardStateProvider } from './contexts';

Expand Down Expand Up @@ -33,6 +34,7 @@ export const LoadingCard = withCardStateProvider(() => {
<Card.Content>
<Card.Alert>{card.error}</Card.Alert>
<LoadingCardContainer />
<CaptchaElement />
</Card.Content>
<Card.Footer />
</Card.Root>
Expand Down
46 changes: 24 additions & 22 deletions packages/clerk-js/src/utils/captcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const getCaptchaToken = async (captchaOptions: {
const { siteKey, scriptUrl, widgetType, invisibleSiteKey } = captchaOptions;
let captchaToken = '',
id = '';
let invisibleWidget = !widgetType || widgetType === 'invisible';
let isInvisibleWidget = !widgetType || widgetType === 'invisible';
let turnstileSiteKey = siteKey;

let widgetDiv: HTMLElement | null = null;
Expand All @@ -112,29 +112,29 @@ export const getCaptchaToken = async (captchaOptions: {
return div;
};

if (invisibleWidget) {
widgetDiv = createInvisibleDOMElement();
} else {
const visibleDiv = document.getElementById(CAPTCHA_ELEMENT_ID);
if (visibleDiv) {
visibleDiv.style.display = 'block';
widgetDiv = visibleDiv;
} else {
console.error('Captcha DOM element not found. Using invisible captcha widget.');
widgetDiv = createInvisibleDOMElement();
invisibleWidget = true;
turnstileSiteKey = invisibleSiteKey;
}
}

const captcha = await loadCaptcha(scriptUrl);
let retries = 0;
const errorCodes: (string | number)[] = [];

const handleCaptchaTokenGeneration = (): Promise<[string, string]> => {
return new Promise((resolve, reject) => {
try {
const id = captcha.render(invisibleWidget ? `.${CAPTCHA_INVISIBLE_CLASSNAME}` : `#${CAPTCHA_ELEMENT_ID}`, {
if (isInvisibleWidget) {
widgetDiv = createInvisibleDOMElement();
} else {
const visibleDiv = document.getElementById(CAPTCHA_ELEMENT_ID);
if (visibleDiv) {
visibleDiv.style.display = 'block';
widgetDiv = visibleDiv;
} else {
console.error('Captcha DOM element not found. Using invisible captcha widget.');
widgetDiv = createInvisibleDOMElement();
isInvisibleWidget = true;
turnstileSiteKey = invisibleSiteKey;
}
}

const id = captcha.render(isInvisibleWidget ? `.${CAPTCHA_INVISIBLE_CLASSNAME}` : `#${CAPTCHA_ELEMENT_ID}`, {
sitekey: turnstileSiteKey,
appearance: 'interaction-only',
retry: 'never',
Expand Down Expand Up @@ -186,12 +186,14 @@ export const getCaptchaToken = async (captchaOptions: {
captchaError: e,
};
} finally {
if (invisibleWidget) {
document.body.removeChild(widgetDiv);
} else {
widgetDiv.style.display = 'none';
if (widgetDiv) {
if (isInvisibleWidget) {
document.body.removeChild(widgetDiv as HTMLElement);
} else {
(widgetDiv as HTMLElement).style.display = 'none';
}
}
}

return { captchaToken, captchaWidgetTypeUsed: invisibleWidget ? 'invisible' : 'smart' };
return { captchaToken, captchaWidgetTypeUsed: isInvisibleWidget ? 'invisible' : 'smart' };
};

0 comments on commit 9968287

Please sign in to comment.