Skip to content

feat(shared): Replace __clerk_db_jwt with __dev_browser in redirects #2431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2023
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
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