-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clerk-js): Set __clerk_suffixed_cookies cookie on custom develop…
…ment domains
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 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
35 changes: 35 additions & 0 deletions
35
packages/clerk-js/src/core/auth/cookies/suffixedCookies.ts
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,35 @@ | ||
import { createCookieHandler } from '@clerk/shared/cookie'; | ||
import { addYears } from '@clerk/shared/date'; | ||
import { getSuffixedCookieName } from '@clerk/shared/keys'; | ||
|
||
import { inCrossOriginIframe } from '../../../utils'; | ||
|
||
const SUFFIXED_COOKIES_COOKIE_NAME = '__clerk_suffixed_cookies'; | ||
|
||
export type SuffixedCookiesCookieHandler = { | ||
set: (enabled: boolean) => void; | ||
}; | ||
|
||
/** | ||
* Create JS cookie as hint for the backend SDKs to allow them identify if the suffixed | ||
* cookies are supported in ClerkJS. | ||
* This cookie will be set by the ClerkJS in development instances with custom development domains | ||
* (eg host != frontendApi eTLD+1 domain) and by FAPI on all the other cases. | ||
*/ | ||
export const createSuffixedCookiesCookie = (publishableKey: string): SuffixedCookiesCookieHandler => { | ||
const suffixedCookiesCookie = createCookieHandler( | ||
getSuffixedCookieName(SUFFIXED_COOKIES_COOKIE_NAME, publishableKey), | ||
); | ||
|
||
const set = (enabled: boolean) => { | ||
const expires = addYears(Date.now(), 1); | ||
const sameSite = inCrossOriginIframe() ? 'None' : 'Lax'; | ||
const secure = window.location.protocol === 'https:'; | ||
|
||
suffixedCookiesCookie.set(enabled.toString(), { expires, sameSite, secure }); | ||
}; | ||
|
||
return { | ||
set, | ||
}; | ||
}; |