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

Fixes an issue where Clerk's internal API client was using the incorrect domain for satellite apps.
29 changes: 29 additions & 0 deletions packages/clerk-js/src/core/__tests__/clerk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2051,6 +2051,35 @@ describe('Clerk singleton', () => {
});
});

describe('Clerk multi-domain', () => {
describe('when development satellite', () => {
it('fapiClient should not use Clerk.domain as its baseUrl', async () => {
const sut = new Clerk(developmentPublishableKey, {
domain: 'satellite.dev',
});
await sut.load({
isSatellite: true,
signInUrl: 'https://primary.dev/sign-in',
});

expect(sut.getFapiClient().buildUrl({ path: '/me' }).href).toContain(`https://${sut.frontendApi}/v1/me`);
});
});

describe('when production satellite', () => {
test('fapiClient should use Clerk.domain as its baseUrl', async () => {
const sut = new Clerk(productionPublishableKey, {
domain: 'satellite.com',
});
await sut.load({
isSatellite: true,
});

expect(sut.getFapiClient().buildUrl({ path: '/me' }).href).toContain('https://clerk.satellite.com/v1/me');
});
});
});

describe('buildUrlWithAuth', () => {
it('builds an absolute url from a relative url in development', async () => {
const sut = new Clerk(developmentPublishableKey);
Expand Down
8 changes: 8 additions & 0 deletions packages/clerk-js/src/core/__tests__/fapiClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ describe('buildUrl(options)', () => {
);
});

it('uses domain from options if production', () => {
expect(
createFapiClient({ ...baseFapiClientOptions, domain: 'clerk.other.com', instanceType: 'production' }).buildUrl({
path: '/foo',
}).href,
).toBe(`https://clerk.other.com/v1/foo?__clerk_api_version=${SUPPORTED_FAPI_VERSION}&_clerk_js_version=test`);
});

it('adds _clerk_session_id as a query parameter if provided and path does not start with client or waitlist', () => {
expect(fapiClient.buildUrl({ path: '/foo', sessionId: 'sess_42' }).href).toBe(
`https://clerk.example.com/v1/foo?__clerk_api_version=${SUPPORTED_FAPI_VERSION}&_clerk_js_version=test&_clerk_session_id=sess_42`,
Expand Down
3 changes: 2 additions & 1 deletion packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,11 @@ export class Clerk implements ClerkInterface {
this.#instanceType = publishableKey.instanceType;

this.#fapiClient = createFapiClient({
domain: (this.instanceType === 'development' && this.isSatellite && this.domain) || undefined,
domain: this.domain,
frontendApi: this.frontendApi,
// this.instanceType is assigned above
instanceType: this.instanceType as InstanceType,
isSatellite: this.isSatellite,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this missing and that's why we had issues, or also domain being undefined was part of the reason ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

domain being passed as undefined was the main reason. I missed that it was used unconditionally when initializing the client.

getSessionId: () => {
return this.session?.id;
},
Expand Down
8 changes: 5 additions & 3 deletions packages/clerk-js/src/core/fapiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type FapiClientOptions = {
proxyUrl?: string;
instanceType: InstanceType;
getSessionId: () => string | undefined;
isSatellite?: boolean;
};

export function createFapiClient(options: FapiClientOptions): FapiClient {
Expand Down Expand Up @@ -114,7 +115,7 @@ export function createFapiClient(options: FapiClientOptions): FapiClient {
searchParams.append('rotating_token_nonce', rotatingTokenNonce);
}

if (options.domain) {
if (options.domain && options.instanceType === 'development' && options.isSatellite) {
searchParams.append('__domain', options.domain);
}

Expand Down Expand Up @@ -144,8 +145,6 @@ export function createFapiClient(options: FapiClientOptions): FapiClient {
function buildUrl(requestInit: FapiRequestInit): URL {
const { path, pathPrefix = 'v1' } = requestInit;

const domainOnlyInProd = options.instanceType === 'production' ? options.domain : '';

if (options.proxyUrl) {
const proxyBase = new URL(options.proxyUrl);
const proxyPath = proxyBase.pathname.slice(1, proxyBase.pathname.length);
Expand All @@ -159,6 +158,9 @@ export function createFapiClient(options: FapiClientOptions): FapiClient {
);
}

// We only use the domain option in production, in development it should always match the frontendApi
const domainOnlyInProd = options.instanceType === 'production' ? options.domain : '';

const baseUrl = `https://${domainOnlyInProd || options.frontendApi}`;

return buildUrlUtil(
Expand Down
Loading