Skip to content

Commit

Permalink
feat(backend): Support multi-domain in dev instances
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef committed Mar 31, 2023
1 parent 48d2ab3 commit 2b8eb75
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 17 additions & 2 deletions packages/backend/src/tokens/interstitialRule.ts
Expand Up @@ -43,6 +43,18 @@ export const crossOriginRequestWithoutHeader: InterstitialRule = options => {
return undefined;
};

export const isPrimaryInDevAndSyncing: InterstitialRule = options => {
const { apiKey, secretKey, isSatellite, searchParams } = options;
const isSyncing = searchParams?.get('__clerk_syncing') === 'true';
const key = secretKey || apiKey;
const isDev = isDevelopmentFromApiKey(key);

if (isDev && !isSatellite && isSyncing) {
return interstitial(options, AuthErrorReason.UnexpectedError);
}
return undefined;
};

export const potentialFirstLoadInDevWhenUATMissing: InterstitialRule = options => {
const { apiKey, secretKey, clientUat } = options;
const key = secretKey || apiKey;
Expand All @@ -54,12 +66,15 @@ export const potentialFirstLoadInDevWhenUATMissing: InterstitialRule = options =
};

export const potentialRequestAfterSignInOrOutFromClerkHostedUiInDev: InterstitialRule = options => {
const { apiKey, secretKey, referrer, host, forwardedHost, forwardedPort, forwardedProto } = options;
const { apiKey, secretKey, referrer, host, forwardedHost, forwardedPort, forwardedProto, isSatellite, searchParams } =
options;
const crossOriginReferrer =
referrer && checkCrossOrigin({ originURL: new URL(referrer), host, forwardedHost, forwardedPort, forwardedProto });
const key = secretKey || apiKey;

if (isDevelopmentFromApiKey(key) && crossOriginReferrer) {
const hasJustSynced = searchParams?.get('__clerk_synced') === 'true';

if (!isSatellite && !hasJustSynced && isDevelopmentFromApiKey(key) && crossOriginReferrer) {
return interstitial(options, AuthErrorReason.CrossOriginReferrer);
}
return undefined;
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/tokens/request.ts
Expand Up @@ -11,6 +11,7 @@ import {
hasValidCookieToken,
hasValidHeaderToken,
isNormalSignedOutState,
isPrimaryInDevAndSyncing,
isSatelliteAndNeedsSyncing,
nonBrowserRequestInDevRule,
potentialFirstLoadInDevWhenUATMissing,
Expand Down Expand Up @@ -100,6 +101,7 @@ export async function authenticateRequest(options: AuthenticateRequestOptions):
crossOriginRequestWithoutHeader,
nonBrowserRequestInDevRule,
isSatelliteAndNeedsSyncing,
isPrimaryInDevAndSyncing,
potentialFirstRequestOnProductionEnvironment,
potentialFirstLoadInDevWhenUATMissing,
potentialRequestAfterSignInOrOutFromClerkHostedUiInDev,
Expand Down

0 comments on commit 2b8eb75

Please sign in to comment.