Skip to content

Commit e9f6241

Browse files
authored
Merge branch 'main' into changeset-release/main
2 parents 317b123 + 4319257 commit e9f6241

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

.changeset/happy-tools-cry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/backend': patch
3+
---
4+
5+
Fix calculation of handshake URL when proxy URL is set on the ClerkProvider

packages/backend/src/tokens/__tests__/handshake.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,35 @@ describe('HandshakeService', () => {
176176
'Missing clerkUrl in authenticateContext',
177177
);
178178
});
179+
180+
it('should use proxy URL when available', () => {
181+
mockAuthenticateContext.proxyUrl = 'https://my-proxy.example.com';
182+
const headers = handshakeService.buildRedirectToHandshake('test-reason');
183+
const location = headers.get(constants.Headers.Location);
184+
if (!location) {
185+
throw new Error('Location header is missing');
186+
}
187+
const url = new URL(location);
188+
189+
expect(url.hostname).toBe('my-proxy.example.com');
190+
expect(url.pathname).toBe('/v1/client/handshake');
191+
expect(url.searchParams.get('redirect_url')).toBe('https://example.com/');
192+
expect(url.searchParams.get(constants.QueryParameters.SuffixedCookies)).toBe('true');
193+
expect(url.searchParams.get(constants.QueryParameters.HandshakeReason)).toBe('test-reason');
194+
});
195+
196+
it('should handle proxy URL with trailing slash', () => {
197+
mockAuthenticateContext.proxyUrl = 'https://my-proxy.example.com/';
198+
const headers = handshakeService.buildRedirectToHandshake('test-reason');
199+
const location = headers.get(constants.Headers.Location);
200+
if (!location) {
201+
throw new Error('Location header is missing');
202+
}
203+
const url = new URL(location);
204+
205+
expect(url.hostname).toBe('my-proxy.example.com');
206+
expect(url.pathname).toBe('/v1/client/handshake');
207+
});
179208
});
180209

181210
describe('handleTokenVerificationErrorInDevelopment', () => {

packages/backend/src/tokens/handshake.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ export class HandshakeService {
136136
const redirectUrl = this.removeDevBrowserFromURL(this.authenticateContext.clerkUrl);
137137
const frontendApiNoProtocol = this.authenticateContext.frontendApi.replace(/http(s)?:\/\//, '');
138138

139-
const url = new URL(`https://${frontendApiNoProtocol}/v1/client/handshake`);
139+
const baseUrl = this.authenticateContext.proxyUrl
140+
? this.authenticateContext.proxyUrl.replace(/\/$/, '')
141+
: `https://${frontendApiNoProtocol}`;
142+
143+
const url = new URL(`${baseUrl}/v1/client/handshake`);
140144
url.searchParams.append('redirect_url', redirectUrl?.href || '');
141145
url.searchParams.append('__clerk_api_version', SUPPORTED_BAPI_VERSION);
142146
url.searchParams.append(

0 commit comments

Comments
 (0)