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/happy-tools-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/backend': patch
---

Fix calculation of handshake URL when proxy URL is set on the ClerkProvider
29 changes: 29 additions & 0 deletions packages/backend/src/tokens/__tests__/handshake.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,35 @@ describe('HandshakeService', () => {
'Missing clerkUrl in authenticateContext',
);
});

it('should use proxy URL when available', () => {
mockAuthenticateContext.proxyUrl = 'https://my-proxy.example.com';
const headers = handshakeService.buildRedirectToHandshake('test-reason');
const location = headers.get(constants.Headers.Location);
if (!location) {
throw new Error('Location header is missing');
}
const url = new URL(location);

expect(url.hostname).toBe('my-proxy.example.com');
expect(url.pathname).toBe('/v1/client/handshake');
expect(url.searchParams.get('redirect_url')).toBe('https://example.com/');
expect(url.searchParams.get(constants.QueryParameters.SuffixedCookies)).toBe('true');
expect(url.searchParams.get(constants.QueryParameters.HandshakeReason)).toBe('test-reason');
});

it('should handle proxy URL with trailing slash', () => {
mockAuthenticateContext.proxyUrl = 'https://my-proxy.example.com/';
const headers = handshakeService.buildRedirectToHandshake('test-reason');
const location = headers.get(constants.Headers.Location);
if (!location) {
throw new Error('Location header is missing');
}
const url = new URL(location);

expect(url.hostname).toBe('my-proxy.example.com');
expect(url.pathname).toBe('/v1/client/handshake');
});
});

describe('handleTokenVerificationErrorInDevelopment', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/backend/src/tokens/handshake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ export class HandshakeService {
const redirectUrl = this.removeDevBrowserFromURL(this.authenticateContext.clerkUrl);
const frontendApiNoProtocol = this.authenticateContext.frontendApi.replace(/http(s)?:\/\//, '');

const url = new URL(`https://${frontendApiNoProtocol}/v1/client/handshake`);
const baseUrl = this.authenticateContext.proxyUrl
? this.authenticateContext.proxyUrl.replace(/\/$/, '')
: `https://${frontendApiNoProtocol}`;

const url = new URL(`${baseUrl}/v1/client/handshake`);
url.searchParams.append('redirect_url', redirectUrl?.href || '');
url.searchParams.append('__clerk_api_version', SUPPORTED_BAPI_VERSION);
url.searchParams.append(
Expand Down
Loading