Skip to content

Commit

Permalink
[Core 1 Backport] Fix cookies on Cypress + Improve script loading fai…
Browse files Browse the repository at this point in the history
…lure logging (#3384)

* fix(clerk-js): Improve logging for CAPTCHA script loading errors (#3374)

* fix(clerk-js): Improve logging for CAPTCHA script loading errors

* fix(clerk-js): Remove the generic CAPTCHA console error

(cherry picked from commit 34befee)

* fix(clerk-js): Use first-party cookies when running on Cypress (#3245)

* fix(clerk-js): Use first-party cookies when running on Cypress

* fix(clerk-js): Create isCypress util

(cherry picked from commit 7b213d5)
  • Loading branch information
anagstef committed May 15, 2024
1 parent 73057f1 commit fce528b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-timers-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Improve logging for CAPTCHA script loading errors
5 changes: 5 additions & 0 deletions .changeset/rotten-rats-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Fix Cypress setting cookies as third-party
11 changes: 6 additions & 5 deletions packages/clerk-js/src/utils/captcha.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { loadScript } from '@clerk/shared/loadScript';
import type { CaptchaWidgetType } from '@clerk/types';

import { clerkFailedToLoadThirdPartyScript } from '../core/errors';

interface RenderOptions {
/**
* Every widget has a sitekey. This sitekey is associated with the corresponding widget configuration and is created upon the widget creation.
Expand Down Expand Up @@ -76,9 +74,12 @@ export async function loadCaptcha(url: string) {
if (!window.turnstile) {
try {
await loadScript(url, { defer: true });
} catch (_) {
} catch {
// Rethrow with specific message
clerkFailedToLoadThirdPartyScript('Cloudflare Turnstile');
console.error('Clerk: Failed to load the CAPTCHA script from the URL: ', url);
throw {
captchaError: 'captcha_script_failed_to_load',
};
}
}
return window.turnstile;
Expand Down Expand Up @@ -112,7 +113,7 @@ export const getCaptchaToken = async (captchaOptions: {
return div;
};

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

Expand Down
8 changes: 7 additions & 1 deletion packages/clerk-js/src/utils/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ export function usesHttps() {
return inBrowser() && window.location.protocol === 'https:';
}

function isCypress() {
return typeof window !== 'undefined' && typeof (window as any).Cypress !== 'undefined';
}

export function inIframe() {
return inBrowser() && window.self !== window.top;
// checks if the current window is an iframe
// excludes the case where the current window runs in a Cypress test
return inBrowser() && window.self !== window.top && !isCypress();
}

export function inCrossOriginIframe() {
Expand Down

0 comments on commit fce528b

Please sign in to comment.