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

Update cookie setting to ensure cookies can be set to be read when an application is embedded in an iframe.
19 changes: 7 additions & 12 deletions packages/clerk-js/src/utils/cookies/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import { devBrowserCookie } from './devBrowser';
import { inittedCookie } from './initted';
import { sessionCookie } from './session';

const DEFAULT_SAME_SITE = 'Lax';
const IFRAME_SAME_SITE = 'None';

const COOKIE_PATH = '/';

export type CookieHandler = ReturnType<typeof createCookieHandler>;
export const createCookieHandler = () => {
// First party cookie helpers
Expand All @@ -22,16 +17,16 @@ export const createCookieHandler = () => {
const setDevBrowserInittedCookie = () =>
inittedCookie.set('1', {
expires: addYears(Date.now(), 1),
sameSite: inSecureCrossOriginIframe() ? IFRAME_SAME_SITE : DEFAULT_SAME_SITE,
sameSite: inSecureCrossOriginIframe() ? 'None' : 'Lax',
secure: inSecureCrossOriginIframe() ? true : undefined,
path: COOKIE_PATH,
path: '/',
});

const removeSessionCookie = () => sessionCookie.remove();

const setSessionCookie = (token: string) => {
const expires = addYears(Date.now(), 1);
const sameSite = inSecureCrossOriginIframe() ? IFRAME_SAME_SITE : DEFAULT_SAME_SITE;
const sameSite = inSecureCrossOriginIframe() ? 'None' : 'Lax';
const secure = inSecureCrossOriginIframe() || window.location.protocol === 'https:';

return sessionCookie.set(token, {
Expand All @@ -47,7 +42,7 @@ export const createCookieHandler = () => {

const setClientUatCookie = (client: ClientResource | undefined) => {
const expires = addYears(Date.now(), 1);
const sameSite = inSecureCrossOriginIframe() ? IFRAME_SAME_SITE : DEFAULT_SAME_SITE;
const sameSite = inSecureCrossOriginIframe() ? 'None' : 'strict';
const secure = inSecureCrossOriginIframe() || window.location.protocol === 'https:';

// '0' indicates the user is signed out
Expand All @@ -67,7 +62,7 @@ export const createCookieHandler = () => {

const setDevBrowserCookie = (jwt: string) => {
const expires = addYears(Date.now(), 1);
const sameSite = inSecureCrossOriginIframe() ? IFRAME_SAME_SITE : DEFAULT_SAME_SITE;
const sameSite = inSecureCrossOriginIframe() ? 'None' : 'Lax';
const secure = inSecureCrossOriginIframe() || window.location.protocol === 'https:';

return devBrowserCookie.set(jwt, {
Expand All @@ -87,9 +82,9 @@ export const createCookieHandler = () => {
const ssoCookie = clientCookie;

const removeAllDevBrowserCookies = () => {
inittedCookie.remove({ path: COOKIE_PATH });
inittedCookie.remove({ path: '/' });
// Delete cookie in a best-effort way by iterating all ETLDs
getAllETLDs().forEach(domain => ssoCookie.remove({ domain, path: COOKIE_PATH }));
getAllETLDs().forEach(domain => ssoCookie.remove({ domain, path: '/' }));
};

return {
Expand Down