diff --git a/.changeset/fluffy-comics-peel.md b/.changeset/fluffy-comics-peel.md new file mode 100644 index 00000000000..8531c19d220 --- /dev/null +++ b/.changeset/fluffy-comics-peel.md @@ -0,0 +1,5 @@ +--- +'@clerk/clerk-js': patch +--- + +Fix double slash in FAPI client URLs when using a proxy configuration (avoids 308 redirects). diff --git a/packages/clerk-js/src/core/__tests__/fapiClient.spec.ts b/packages/clerk-js/src/core/__tests__/fapiClient.spec.ts index 728b259fe94..7aa96b3368f 100644 --- a/packages/clerk-js/src/core/__tests__/fapiClient.spec.ts +++ b/packages/clerk-js/src/core/__tests__/fapiClient.spec.ts @@ -21,6 +21,13 @@ const fapiClientWithProxy = createFapiClient({ proxyUrl, }); +const proxyUrlWithTrailingSlash = 'https://clerk.com/api/__clerk/'; + +const fapiClientWithProxyTrailingSlash = createFapiClient({ + ...baseFapiClientOptions, + proxyUrl: proxyUrlWithTrailingSlash, +}); + type RecursivePartial = { [P in keyof T]?: RecursivePartial; }; @@ -79,6 +86,20 @@ describe('buildUrl(options)', () => { ); }); + it('returns the correct URL when proxy URL has a trailing slash', () => { + // The expected URL should NOT have double slashes after __clerk + expect(fapiClientWithProxyTrailingSlash.buildUrl({ path: '/foo' }).href).toBe( + `https://clerk.com/api/__clerk/v1/foo?__clerk_api_version=${SUPPORTED_FAPI_VERSION}&_clerk_js_version=test`, + ); + }); + + it('handles complex paths correctly with proxy URL with trailing slash', () => { + const path = '/client/sign_ins/sia_123/prepare_first_factor'; + expect(fapiClientWithProxyTrailingSlash.buildUrl({ path }).href).toBe( + `https://clerk.com/api/__clerk/v1${path}?__clerk_api_version=${SUPPORTED_FAPI_VERSION}&_clerk_js_version=test`, + ); + }); + it('uses domain from options if production', () => { expect( createFapiClient({ ...baseFapiClientOptions, domain: 'clerk.other.com', instanceType: 'production' }).buildUrl({ diff --git a/packages/clerk-js/src/core/fapiClient.ts b/packages/clerk-js/src/core/fapiClient.ts index d12cce9a181..2207aca0251 100644 --- a/packages/clerk-js/src/core/fapiClient.ts +++ b/packages/clerk-js/src/core/fapiClient.ts @@ -149,7 +149,10 @@ export function createFapiClient(options: FapiClientOptions): FapiClient { if (options.proxyUrl) { const proxyBase = new URL(options.proxyUrl); - const proxyPath = proxyBase.pathname.slice(1, proxyBase.pathname.length); + let proxyPath = proxyBase.pathname.slice(1); + if (proxyPath.endsWith('/')) { + proxyPath = proxyPath.slice(0, -1); + } return buildUrlUtil( { base: proxyBase.origin,