Repository navigation
Expand file tree
/
Copy pathloadOptions.ts
More file actions
71 lines (63 loc) · 2.57 KB
/
Copy pathloadOptions.ts
File metadata and controls
71 lines (63 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { createClerkRequest } from '@clerk/backend/internal';
import { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey';
import { isDevelopmentFromSecretKey } from '@clerk/shared/keys';
import { isHttpOrHttps, isProxyUrlRelative } from '@clerk/shared/proxy';
import { handleValueOrFn } from '@clerk/shared/utils';
import { errorThrower } from '../utils';
import { getEnvVariable, getPublicEnvVariables } from '../utils/env';
import {
CLERK_JWT_KEY,
DOMAIN,
IS_SATELLITE,
PROXY_URL,
PUBLISHABLE_KEY,
SECRET_KEY,
SIGN_IN_URL,
SIGN_UP_URL,
} from './constants';
import type { LoaderOptions } from './types';
import { patchRequest } from './utils';
export const loadOptions = (request: Request, overrides: LoaderOptions = {}) => {
const clerkRequest = createClerkRequest(patchRequest(request));
const secretKey = overrides.secretKey || SECRET_KEY;
const publishableKey = overrides.publishableKey || PUBLISHABLE_KEY;
const jwtKey = overrides.jwtKey || CLERK_JWT_KEY;
const apiUrl = getEnvVariable('CLERK_API_URL') || apiUrlFromPublishableKey(publishableKey);
const domain = handleValueOrFn(overrides.domain, new URL(request.url)) || DOMAIN;
const isSatellite = handleValueOrFn(overrides.isSatellite, new URL(request.url)) || IS_SATELLITE;
const relativeOrAbsoluteProxyUrl = handleValueOrFn(overrides?.proxyUrl, clerkRequest.clerkUrl, PROXY_URL);
const signInUrl = overrides.signInUrl || SIGN_IN_URL;
const signUpUrl = overrides.signUpUrl || SIGN_UP_URL;
const afterSignInUrl = overrides.afterSignInUrl || getPublicEnvVariables().afterSignInUrl;
const afterSignUpUrl = overrides.afterSignUpUrl || getPublicEnvVariables().afterSignUpUrl;
let proxyUrl;
if (!!relativeOrAbsoluteProxyUrl && isProxyUrlRelative(relativeOrAbsoluteProxyUrl)) {
proxyUrl = new URL(relativeOrAbsoluteProxyUrl, clerkRequest.clerkUrl).toString();
} else {
proxyUrl = relativeOrAbsoluteProxyUrl;
}
if (!secretKey) {
throw errorThrower.throw('Clerk: no secret key provided');
}
if (isSatellite && !proxyUrl && !domain) {
throw errorThrower.throw('Clerk: satellite mode requires a proxy URL or domain');
}
if (isSatellite && !isHttpOrHttps(signInUrl) && isDevelopmentFromSecretKey(secretKey)) {
throw errorThrower.throw('Clerk: satellite mode requires a sign-in URL in production');
}
return {
// used to append options that are not initialized from env
...overrides,
secretKey,
publishableKey,
jwtKey,
apiUrl,
domain,
isSatellite,
proxyUrl,
signInUrl,
signUpUrl,
afterSignInUrl,
afterSignUpUrl,
};
};