-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(clerk-js): Simplify dev browser handling
1. Work only with URL based session syncing dev instances and drop support for cookie-based syncing dev instances. 2. Align nomenclature across cookie, hearer and search param names 3. Drop cookie handler in favor of dedicated cookie handlers, one for each cookie (__clerk_db_jwt, __session, __client_uat)
- Loading branch information
1 parent
f58a994
commit 3e66133
Showing
22 changed files
with
234 additions
and
552 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { getDevBrowserJWTFromURL, setDevBrowserJWTInURL } from '@clerk/shared/devBrowser'; | ||
|
||
import { isDevOrStagingUrl } from '../utils'; | ||
import { getDevBrowserCookie, removeDevBrowserCookie, setDevBrowserCookie } from '../utils/cookies/devBrowser'; | ||
import { clerkErrorDevInitFailed } from './errors'; | ||
import type { FapiClient } from './fapiClient'; | ||
|
||
export interface DevBrowser { | ||
clear(): void; | ||
|
||
setup(): Promise<void>; | ||
|
||
getDevBrowserJWT(): string | undefined; | ||
|
||
setDevBrowserJWT(jwt: string): void; | ||
|
||
removeDevBrowserJWT(): void; | ||
} | ||
|
||
export type CreateDevBrowserOptions = { | ||
frontendApi: string; | ||
fapiClient: FapiClient; | ||
}; | ||
|
||
export function createDevBrowser({ frontendApi, fapiClient }: CreateDevBrowserOptions): DevBrowser { | ||
function getDevBrowserJWT() { | ||
return getDevBrowserCookie(); | ||
} | ||
|
||
function setDevBrowserJWT(jwt: string) { | ||
setDevBrowserCookie(jwt); | ||
} | ||
|
||
function removeDevBrowserJWT() { | ||
removeDevBrowserCookie(); | ||
} | ||
|
||
function clear() { | ||
removeDevBrowserJWT(); | ||
} | ||
|
||
async function setup(): Promise<void> { | ||
if (!isDevOrStagingUrl(frontendApi)) { | ||
return; | ||
} | ||
|
||
// 1. Set network interceptors to Pass dev | ||
fapiClient.onBeforeRequest(request => { | ||
const devBrowserJWT = getDevBrowserJWT(); | ||
if (devBrowserJWT && request?.url) { | ||
request.url = setDevBrowserJWTInURL(request.url, devBrowserJWT, true); | ||
} | ||
}); | ||
|
||
fapiClient.onAfterResponse((_, response) => { | ||
const newDevBrowserJWT = response?.headers?.get('Clerk-Db-Jwt'); | ||
if (newDevBrowserJWT) { | ||
setDevBrowserJWT(newDevBrowserJWT); | ||
} | ||
}); | ||
|
||
// 1. If a cookie already exists, it might have SameSite=Strict. Re-set it to make sure it has SameSite=Lax | ||
const existingDevBrowserCookie = getDevBrowserCookie(); | ||
if (existingDevBrowserCookie) { | ||
removeDevBrowserCookie(); | ||
setDevBrowserCookie(existingDevBrowserCookie); | ||
} | ||
|
||
// 2. Get the JWT from hash or search parameters when the redirection comes from AP | ||
const devBrowserToken = getDevBrowserJWTFromURL(new URL(window.location.href)); | ||
if (devBrowserToken) { | ||
setDevBrowserJWT(devBrowserToken); | ||
return; | ||
} | ||
|
||
// 3. If no JWT is found in the first step, check if a JWT is already available in the JS cookie | ||
if (getDevBrowserCookie()) { | ||
return; | ||
} | ||
|
||
// 4. Otherwise, fetch a new DevBrowser JWT from FAPI and cache it | ||
const createDevBrowserUrl = fapiClient.buildUrl({ | ||
path: '/dev_browser', | ||
}); | ||
|
||
const resp = await fetch(createDevBrowserUrl.toString(), { | ||
method: 'POST', | ||
}); | ||
|
||
if (!resp.ok) { | ||
clerkErrorDevInitFailed(); | ||
} | ||
|
||
const data = await resp.json(); | ||
setDevBrowserJWT(data?.token); | ||
} | ||
|
||
return { | ||
clear, | ||
setup, | ||
getDevBrowserJWT, | ||
setDevBrowserJWT, | ||
removeDevBrowserJWT, | ||
}; | ||
} |
Oops, something went wrong.