Skip to content

Commit 42e0549

Browse files
committed
chore(clerk-sdk-node): Create a loadInterstitial utility
1 parent 167105d commit 42e0549

5 files changed

Lines changed: 40 additions & 42 deletions

File tree

packages/nextjs/src/server/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Missing domain and proxyUrl. A satellite application needs to specify a domain o
1010

1111
export const missingSignInUrlInDev = `
1212
Invalid signInUrl. A satellite application requires a signInUrl for development instances.
13-
Check if signInUrl is missing from your configuration or it is not a absolute URL
13+
Check if signInUrl is missing from your configuration or if it is not an absolute URL
1414
1515
1) With middleware
1616
e.g. export default withClerkMiddleware(req => {...}, {signInUrl:'SOME_URL',isSatellite:true});

packages/remix/src/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,4 @@ export const satelliteAndMissingProxyUrlAndDomain = createErrorMessage(
8181

8282
export const satelliteAndMissingSignInUrl = createErrorMessage(`
8383
Invalid signInUrl. A satellite application requires a signInUrl for development instances.
84-
Check if signInUrl is missing from your configuration or it is not a absolute URL.`);
84+
Check if signInUrl is missing from your configuration or if it is not an absolute URL.`);

packages/sdk-node/src/authenticateRequest.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,32 @@ const parseCookies = (req: IncomingMessage) => {
1010
return cookie.parse(req.headers['cookie'] || '');
1111
};
1212

13+
type ClerkClient = ReturnType<typeof Clerk>;
14+
15+
export async function loadInterstitial({
16+
clerkClient,
17+
requestState,
18+
}: {
19+
clerkClient: ClerkClient;
20+
requestState: RequestState;
21+
}) {
22+
/**
23+
* When publishable key or frontendApi is present utilize the localInterstitial method
24+
* and avoid the extra network call
25+
*/
26+
if (requestState.publishableKey || requestState.frontendApi) {
27+
return clerkClient.localInterstitial({
28+
frontendApi: requestState.frontendApi,
29+
publishableKey: requestState.publishableKey,
30+
proxyUrl: requestState.proxyUrl,
31+
signInUrl: requestState.signInUrl,
32+
isSatellite: requestState.isSatellite,
33+
domain: requestState.domain,
34+
});
35+
}
36+
return await clerkClient.remotePrivateInterstitial();
37+
}
38+
1339
export const authenticateRequest = (opts: {
1440
clerkClient: ReturnType<typeof Clerk>;
1541
apiKey: string;
@@ -116,6 +142,6 @@ const satelliteAndMissingProxyUrlAndDomain =
116142
'Missing domain and proxyUrl. A satellite application needs to specify a domain or a proxyUrl';
117143
const satelliteAndMissingSignInUrl = `
118144
Invalid signInUrl. A satellite application requires a signInUrl for development instances.
119-
Check if signInUrl is missing from your configuration or it is not a absolute URL.`;
145+
Check if signInUrl is missing from your configuration or if it is not an absolute URL.`;
120146
const missingProto =
121-
"Cannot determine the request protocol. Please make sure you've set the X-Forwarded-Proto header with the request protocol (http or https).";
147+
"Cannot determine the request protocol. Please ensure you've set the X-Forwarded-Proto header with the request protocol (http or https).";

packages/sdk-node/src/clerkExpressRequireAuth.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
decorateResponseWithObservabilityHeaders,
66
handleInterstitialCase,
77
handleUnknownCase,
8+
loadInterstitial,
89
} from './authenticateRequest';
910
import type { ClerkMiddlewareOptions, MiddlewareRequireAuthProp, RequireAuthProp } from './types';
1011

@@ -43,25 +44,10 @@ export const createClerkExpressRequireAuth = (createOpts: CreateClerkExpressMidd
4344
return handleUnknownCase(res, requestState);
4445
}
4546
if (requestState.isInterstitial) {
46-
let interstitial;
47-
48-
/**
49-
* This is a step for deprecating the usage of `remotePrivateInterstitial`
50-
* For the multi-domain feature and when frontendApi is set prefer the localInterstitial
51-
*/
52-
if (requestState.publishableKey || requestState.frontendApi) {
53-
interstitial = clerkClient.localInterstitial({
54-
frontendApi: requestState.frontendApi,
55-
publishableKey: requestState.publishableKey,
56-
proxyUrl: requestState.proxyUrl,
57-
signInUrl: requestState.signInUrl,
58-
isSatellite: requestState.isSatellite,
59-
domain: requestState.domain,
60-
});
61-
} else {
62-
interstitial = await clerkClient.remotePrivateInterstitial();
63-
}
64-
47+
const interstitial = await loadInterstitial({
48+
clerkClient,
49+
requestState,
50+
});
6551
return handleInterstitialCase(res, requestState, interstitial);
6652
}
6753

packages/sdk-node/src/clerkExpressWithAuth.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
decorateResponseWithObservabilityHeaders,
44
handleInterstitialCase,
55
handleUnknownCase,
6+
loadInterstitial,
67
} from './authenticateRequest';
78
import type { CreateClerkExpressMiddlewareOptions } from './clerkExpressRequireAuth';
89
import type { ClerkMiddlewareOptions, MiddlewareWithAuthProp, WithAuthProp } from './types';
@@ -25,25 +26,10 @@ export const createClerkExpressWithAuth = (createOpts: CreateClerkExpressMiddlew
2526
return handleUnknownCase(res, requestState);
2627
}
2728
if (requestState.isInterstitial) {
28-
let interstitial;
29-
30-
/**
31-
* This is a step for deprecating the usage of `remotePrivateInterstitial`
32-
* For the multi-domain feature and when frontendApi is set prefer the localInterstitial
33-
*/
34-
if (requestState.publishableKey || requestState.frontendApi) {
35-
interstitial = clerkClient.localInterstitial({
36-
frontendApi: requestState.frontendApi,
37-
publishableKey: requestState.publishableKey,
38-
proxyUrl: requestState.proxyUrl,
39-
signInUrl: requestState.signInUrl,
40-
isSatellite: requestState.isSatellite,
41-
domain: requestState.domain,
42-
});
43-
} else {
44-
interstitial = await clerkClient.remotePrivateInterstitial();
45-
}
46-
29+
const interstitial = await loadInterstitial({
30+
clerkClient,
31+
requestState,
32+
});
4733
return handleInterstitialCase(res, requestState, interstitial);
4834
}
4935

0 commit comments

Comments
 (0)