From 914eb761e89d32e13ef12db2df255783ff327471 Mon Sep 17 00:00:00 2001 From: Bryce Kalow Date: Thu, 9 May 2024 16:23:58 -0500 Subject: [PATCH] fix(clerk-js): Handle single part hostnames in getCookieDomain() --- .changeset/eighty-pens-chew.md | 5 +++++ .../src/utils/cookies/__tests__/getCookieDomain.test.ts | 4 ++++ packages/clerk-js/src/utils/cookies/getCookieDomain.ts | 5 +++++ 3 files changed, 14 insertions(+) create mode 100644 .changeset/eighty-pens-chew.md diff --git a/.changeset/eighty-pens-chew.md b/.changeset/eighty-pens-chew.md new file mode 100644 index 00000000000..38b0fa17236 --- /dev/null +++ b/.changeset/eighty-pens-chew.md @@ -0,0 +1,5 @@ +--- +'@clerk/clerk-js': patch +--- + +Fix a bug where client_uat domain detection would not handle hostnames with a single part. This impacts locally defined custom hostnames used as aliases. diff --git a/packages/clerk-js/src/utils/cookies/__tests__/getCookieDomain.test.ts b/packages/clerk-js/src/utils/cookies/__tests__/getCookieDomain.test.ts index f9a8b6457f7..5f2b335f22e 100644 --- a/packages/clerk-js/src/utils/cookies/__tests__/getCookieDomain.test.ts +++ b/packages/clerk-js/src/utils/cookies/__tests__/getCookieDomain.test.ts @@ -47,6 +47,10 @@ describe('getCookieDomain', () => { } }); + it('handles custom hosts defined locally', () => { + expect(getCookieDomain('bryce-local')).toBe('bryce-local'); + }); + it('returns undefined if the domain could not be determined', () => { const handler: CookieHandler = { get: jest.fn().mockReturnValue(undefined), diff --git a/packages/clerk-js/src/utils/cookies/getCookieDomain.ts b/packages/clerk-js/src/utils/cookies/getCookieDomain.ts index e7aa37331a8..492c2beab12 100644 --- a/packages/clerk-js/src/utils/cookies/getCookieDomain.ts +++ b/packages/clerk-js/src/utils/cookies/getCookieDomain.ts @@ -20,6 +20,11 @@ export function getCookieDomain(hostname = window.location.hostname, cookieHandl const hostnameParts = hostname.split('.'); + // Account for custom hosts defined locally, e.g. 127.0.0.1 -> local-app + if (hostnameParts.length === 1) { + return hostname; + } + // we know for sure that the first entry is definitely a TLD, skip it for (let i = hostnameParts.length - 2; i >= 0; i--) { const eTLD = hostnameParts.slice(i).join('.');