-
Notifications
You must be signed in to change notification settings - Fork 408
fix(clerk-js,backend): Do not depend on _clerk_synced to terminate satellite sync flow #4516
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@clerk/clerk-js': patch | ||
| '@clerk/backend': patch | ||
| --- | ||
|
|
||
| Fixes satellite syncing when both the satellite and the primary apps use server-side enabled frameworks like NextJS |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -124,7 +124,10 @@ export async function authenticateRequest( | |
|
|
||
| const url = new URL(`https://${frontendApiNoProtocol}/v1/client/handshake`); | ||
| url.searchParams.append('redirect_url', redirectUrl?.href || ''); | ||
| url.searchParams.append('suffixed_cookies', authenticateContext.suffixedCookies.toString()); | ||
| url.searchParams.append( | ||
| constants.QueryParameters.SuffixedCookies, | ||
| authenticateContext.usesSuffixedCookies().toString(), | ||
| ); | ||
| url.searchParams.append(constants.QueryParameters.HandshakeReason, handshakeReason); | ||
|
|
||
| if (authenticateContext.instanceType === 'development' && authenticateContext.devBrowserToken) { | ||
|
|
@@ -472,11 +475,6 @@ ${error.getFullMessage()}`, | |
| const hasSessionToken = !!authenticateContext.sessionTokenInCookie; | ||
| const hasDevBrowserToken = !!authenticateContext.devBrowserToken; | ||
|
|
||
| const isRequestEligibleForMultiDomainSync = | ||
| authenticateContext.isSatellite && | ||
| authenticateContext.secFetchDest === 'document' && | ||
| !authenticateContext.clerkUrl.searchParams.has(constants.QueryParameters.ClerkSynced); | ||
|
|
||
| /** | ||
| * If we have a handshakeToken, resolve the handshake and attempt to return a definitive signed in or signed out state. | ||
| */ | ||
|
|
@@ -512,6 +510,9 @@ ${error.getFullMessage()}`, | |
| return handleMaybeHandshakeStatus(authenticateContext, AuthErrorReason.DevBrowserSync, ''); | ||
| } | ||
|
|
||
| const isRequestEligibleForMultiDomainSync = | ||
| authenticateContext.isSatellite && authenticateContext.secFetchDest === 'document'; | ||
|
|
||
|
Comment on lines
+513
to
+515
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the actual change, correct ?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. I will check the clerk_go side shortly but we can push these changes independently
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The clerk-js change is also important as we couldn't remove the param before for nextjs apps. |
||
| /** | ||
| * Begin multi-domain sync flows | ||
| */ | ||
|
|
@@ -529,17 +530,19 @@ ${error.getFullMessage()}`, | |
| constants.QueryParameters.ClerkRedirectUrl, | ||
| authenticateContext.clerkUrl.toString(), | ||
| ); | ||
| const authErrReason = AuthErrorReason.SatelliteCookieNeedsSyncing; | ||
| redirectURL.searchParams.append(constants.QueryParameters.HandshakeReason, authErrReason); | ||
|
|
||
| redirectURL.searchParams.append( | ||
| constants.QueryParameters.HandshakeReason, | ||
| AuthErrorReason.SatelliteCookieNeedsSyncing, | ||
| ); | ||
| const headers = new Headers({ [constants.Headers.Location]: redirectURL.toString() }); | ||
| return handleMaybeHandshakeStatus(authenticateContext, authErrReason, '', headers); | ||
| return handleMaybeHandshakeStatus(authenticateContext, AuthErrorReason.SatelliteCookieNeedsSyncing, '', headers); | ||
| } | ||
|
|
||
| // Multi-domain development sync flow | ||
| const redirectUrl = new URL(authenticateContext.clerkUrl).searchParams.get( | ||
| constants.QueryParameters.ClerkRedirectUrl, | ||
| ); | ||
|
|
||
| if (authenticateContext.instanceType === 'development' && !authenticateContext.isSatellite && redirectUrl) { | ||
| // Dev MD sync from primary, redirect back to satellite w/ dev browser query param | ||
| const redirectBackToSatelliteUrl = new URL(redirectUrl); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,7 +99,7 @@ import { assertNoLegacyProp } from '../utils/assertNoLegacyProp'; | |
| import { memoizeListenerCallback } from '../utils/memoizeStateListenerCallback'; | ||
| import { RedirectUrls } from '../utils/redirectUrls'; | ||
| import { AuthCookieService } from './auth/AuthCookieService'; | ||
| import { CLERK_SATELLITE_URL, CLERK_SYNCED, ERROR_CODES } from './constants'; | ||
| import { CLERK_SATELLITE_URL, CLERK_SUFFIXED_COOKIES, CLERK_SYNCED, ERROR_CODES } from './constants'; | ||
| import { | ||
| clerkErrorInitFailed, | ||
| clerkInvalidSignInUrlFormat, | ||
|
|
@@ -1635,9 +1635,6 @@ export class Clerk implements ClerkInterface { | |
| return this.navigate(to); | ||
| } | ||
|
|
||
| #hasJustSynced = () => getClerkQueryParam(CLERK_SYNCED) === 'true'; | ||
| #clearJustSynced = () => removeClerkQueryParam(CLERK_SYNCED); | ||
|
|
||
| #buildSyncUrlForDevelopmentInstances = (): string => { | ||
| const searchParams = new URLSearchParams({ | ||
| [CLERK_SATELLITE_URL]: window.location.href, | ||
|
|
@@ -1661,8 +1658,7 @@ export class Clerk implements ClerkInterface { | |
| }; | ||
|
|
||
| #shouldSyncWithPrimary = (): boolean => { | ||
| if (this.#hasJustSynced()) { | ||
| this.#clearJustSynced(); | ||
| if (getClerkQueryParam(CLERK_SYNCED) === 'true') { | ||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -1818,7 +1814,7 @@ export class Clerk implements ClerkInterface { | |
| } | ||
| } | ||
|
|
||
| this.#clearHandshakeFromUrl(); | ||
| this.#clearClerkQueryParams(); | ||
|
|
||
| this.#handleImpersonationFab(); | ||
| return true; | ||
|
|
@@ -2010,8 +2006,12 @@ export class Clerk implements ClerkInterface { | |
| * The handshake payload is transported in the URL in development. In cases where FAPI is returning the handshake payload, but Clerk is being used in a client-only application, | ||
| * we remove the handshake associated parameters as they are not necessary. | ||
| */ | ||
| #clearHandshakeFromUrl = () => { | ||
| #clearClerkQueryParams = () => { | ||
| try { | ||
| removeClerkQueryParam(CLERK_SYNCED); | ||
| // @nikos: we're looking into dropping this param completely | ||
| // in the meantime, we're removing it here to keep the URL clean | ||
| removeClerkQueryParam(CLERK_SUFFIXED_COOKIES); | ||
|
Comment on lines
+2012
to
+2014
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should never make it to the client, I'm pretty sure it being added to the redirect is a bug in FAPI that needs to be fixed.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's exactly the case, I'll get to the bottom of it tomorrow |
||
| removeClerkQueryParam('__clerk_handshake'); | ||
| removeClerkQueryParam('__clerk_help'); | ||
| } catch (_) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the same code - git diff messes with formatting here.
I dropped the suffixedCookies prop that was getting initialized in the ctor in favor of a simple function to simplify things a little.