-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathClerkProvider.tsx
More file actions
87 lines (76 loc) · 2.91 KB
/
ClerkProvider.tsx
File metadata and controls
87 lines (76 loc) · 2.91 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { ClerkProvider as ReactClerkProvider } from '@clerk/react';
import type { Ui } from '@clerk/react/internal';
import { ScriptOnce } from '@tanstack/react-router';
import { getGlobalStartContext } from '@tanstack/react-start';
import { useEffect } from 'react';
import { isClient } from '../utils';
import { ClerkOptionsProvider } from './OptionsContext';
import type { TanstackStartClerkProviderProps } from './types';
import { useAwaitableNavigate } from './useAwaitableNavigate';
import { mergeWithPublicEnvs, parseUrlForNavigation, pickFromClerkInitState } from './utils';
export * from '@clerk/react';
const SDK_METADATA = {
name: PACKAGE_NAME,
version: PACKAGE_VERSION,
};
const awaitableNavigateRef: { current: ReturnType<typeof useAwaitableNavigate> | undefined } = { current: undefined };
export function ClerkProvider<TUi extends Ui = Ui>({
children,
...providerProps
}: TanstackStartClerkProviderProps<TUi>): JSX.Element {
const awaitableNavigate = useAwaitableNavigate();
// @ts-expect-error: Untyped internal Clerk initial state
const clerkInitialState = getGlobalStartContext()?.clerkInitialState ?? {};
useEffect(() => {
awaitableNavigateRef.current = awaitableNavigate;
}, [awaitableNavigate]);
const clerkInitState = isClient() ? (window as any).__clerk_init_state : clerkInitialState;
const { clerkSsrState, __keylessClaimUrl, __keylessApiKeysUrl, ...restInitState } = pickFromClerkInitState(
clerkInitState?.__internal_clerk_state,
);
const mergedProps = {
...mergeWithPublicEnvs(restInitState),
...providerProps,
};
// Add keyless mode props if present
const keylessProps = __keylessClaimUrl
? {
__internal_keyless_claimKeylessApplicationUrl: __keylessClaimUrl,
__internal_keyless_copyInstanceKeysUrl: __keylessApiKeysUrl,
}
: {};
return (
<>
<ScriptOnce>{`window.__clerk_init_state = ${JSON.stringify(clerkInitialState)};`}</ScriptOnce>
<ClerkOptionsProvider options={mergedProps}>
<ReactClerkProvider
initialState={clerkSsrState}
sdkMetadata={SDK_METADATA}
routerPush={(to: string) => {
const { search, hash, ...rest } = parseUrlForNavigation(to, window.location.origin);
return awaitableNavigateRef.current?.({
...rest,
search: search as any,
hash,
replace: false,
});
}}
routerReplace={(to: string) => {
const { search, hash, ...rest } = parseUrlForNavigation(to, window.location.origin);
return awaitableNavigateRef.current?.({
...rest,
search: search as any,
hash,
replace: true,
});
}}
{...mergedProps}
{...keylessProps}
>
{children}
</ReactClerkProvider>
</ClerkOptionsProvider>
</>
);
}
ClerkProvider.displayName = 'ClerkProvider';