Skip to content

Commit

Permalink
feat(shared): Use both __clerk_db_jwt and __dev_browser in redire…
Browse files Browse the repository at this point in the history
…cts (#2431)
  • Loading branch information
dimkl committed Dec 22, 2023
1 parent 3ece3f8 commit 0bf0bdd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/wise-clocks-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/shared': minor
---

Use both `__clerk_db_jwt` and `__dev_browser` search params to sync dev browser between application and Account Portal in development instances.
This change is required to support the next major version of the ClerkJS.
6 changes: 3 additions & 3 deletions packages/clerk-js/src/core/clerk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2113,7 +2113,7 @@ describe('Clerk singleton', () => {
await sut.load();

const url = sut.buildUrlWithAuth('https://example.com/some-path', { useQueryParam: true });
expect(url).toBe('https://example.com/some-path?__dev_session=deadbeef');
expect(url).toBe('https://example.com/some-path?__dev_session=deadbeef&__clerk_db_jwt=deadbeef');
});

it('uses the query param to propagate the dev_browser JWT to Account Portal pages on dev - non-kima', async () => {
Expand All @@ -2122,7 +2122,7 @@ describe('Clerk singleton', () => {
await sut.load();

const url = sut.buildUrlWithAuth('https://accounts.abcef.12345.dev.lclclerk.com');
expect(url).toBe('https://accounts.abcef.12345.dev.lclclerk.com/?__dev_session=deadbeef');
expect(url).toBe('https://accounts.abcef.12345.dev.lclclerk.com/?__dev_session=deadbeef&__clerk_db_jwt=deadbeef');
});

it('uses the query param to propagate the dev_browser JWT to Account Portal pages on dev - kima', async () => {
Expand All @@ -2131,7 +2131,7 @@ describe('Clerk singleton', () => {
await sut.load();

const url = sut.buildUrlWithAuth('https://rested-anemone-14.accounts.dev');
expect(url).toBe('https://rested-anemone-14.accounts.dev/?__dev_session=deadbeef');
expect(url).toBe('https://rested-anemone-14.accounts.dev/?__dev_session=deadbeef&__clerk_db_jwt=deadbeef');
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/src/server/authMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ describe('Dev Browser JWT when redirecting to cross origin', function () {

expect(resp?.status).toEqual(307);
expect(resp?.headers.get('location')).toEqual(
'https://accounts.included.katydid-92.lcl.dev/sign-in?redirect_url=https%3A%2F%2Fwww.clerk.com%2Fprotected&__dev_session=test_jwt',
'https://accounts.included.katydid-92.lcl.dev/sign-in?redirect_url=https%3A%2F%2Fwww.clerk.com%2Fprotected&__dev_session=test_jwt&__clerk_db_jwt=test_jwt',
);
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(authenticateRequest).toBeCalled();
Expand Down
18 changes: 14 additions & 4 deletions packages/shared/src/__tests__/devbrowser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@ describe('setDevBrowserJWTInURL(url, jwt)', () => {
['/foo?bar=42#qux', 'deadbeef', false, '/foo?bar=42#qux__clerk_db_jwt[deadbeef]'],
['/foo#__clerk_db_jwt[deadbeef]', 'deadbeef', false, '/foo#__clerk_db_jwt[deadbeef]'],
['/foo?bar=42#qux__clerk_db_jwt[deadbeef]', 'deadbeef', false, '/foo?bar=42#qux__clerk_db_jwt[deadbeef]'],
['/foo', 'deadbeef', true, '/foo?__dev_session=deadbeef'],
['/foo?bar=42', 'deadbeef', true, '/foo?bar=42&__dev_session=deadbeef'],
['/foo?bar=42&__clerk_db_jwt=deadbeef', 'deadbeef', true, '/foo?bar=42&__dev_session=deadbeef'],
['/foo?bar=42&__dev_session=deadbeef', 'deadbeef', true, '/foo?bar=42&__dev_session=deadbeef'],
['/foo', 'deadbeef', true, '/foo?__dev_session=deadbeef&__clerk_db_jwt=deadbeef'],
['/foo?bar=42', 'deadbeef', true, '/foo?bar=42&__dev_session=deadbeef&__clerk_db_jwt=deadbeef'],
[
'/foo?bar=42&__clerk_db_jwt=deadbeef',
'deadbeef',
true,
'/foo?bar=42&__dev_session=deadbeef&__clerk_db_jwt=deadbeef',
],
[
'/foo?bar=42&__dev_session=deadbeef',
'deadbeef',
true,
'/foo?bar=42&__dev_session=deadbeef&__clerk_db_jwt=deadbeef',
],
];

test.each(testCases)(
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/devBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export function setDevBrowserJWTInURL(url: URL, jwt: string, asQueryParam: boole

if (jwtToSet) {
if (asQueryParam) {
// Temporarily add the dev browser jwt to both the `__clerk_db_jwt` and `__dev_session`
resultURL.searchParams.append(DEV_BROWSER_SSO_JWT_PARAMETER, jwtToSet);
resultURL.searchParams.append(DEV_BROWSER_JWT_MARKER, jwtToSet);
} else {
resultURL.hash = resultURL.hash + `${DEV_BROWSER_JWT_MARKER}[${jwtToSet}]`;
}
Expand Down

0 comments on commit 0bf0bdd

Please sign in to comment.