import '../polyfills'; import type { ClerkProviderProps as ReactClerkProviderProps } from '@clerk/react'; import { InternalClerkProvider as ClerkReactProvider, type Ui } from '@clerk/react/internal'; import { useEffect, useMemo, useRef } from 'react'; import type { TokenCache } from '../cache/types'; import { ClerkExpoModule } from '../utils/native-module'; import { isNative, isWeb } from '../utils/runtime'; import { maybeCompleteAuthSession } from './maybeCompleteAuthSession'; import { createNativeClientSync, type SyncableClerk } from './nativeClientSync'; import { getClerkInstance } from './singleton'; import type { BuildClerkOptions } from './singleton/types'; export type ClerkProviderProps = Omit, 'publishableKey'> & { /** * Your Clerk publishable key, available in the Clerk Dashboard. * This is required for React Native / Expo apps. Environment variables inside node_modules * are not inlined during production builds, so the key must be passed explicitly. * * @example * ```tsx * const publishableKey = process.env.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY!; * * ``` */ publishableKey: string; /** * The token cache is used to persist the client JWT that identifies this device to Clerk. Clerk keeps it in memory by default, however it is recommended to use a token cache backed by secure storage for production applications. * @see https://clerk.com/docs/quickstarts/expo#configure-the-token-cache-with-expo */ tokenCache?: TokenCache; /** * Note: Passkey support in Expo is currently in a limited rollout phase. * If you're interested in using this feature, please contact us for early access or additional details. * * @experimental This API is experimental and may change at any moment. */ __experimental_passkeys?: BuildClerkOptions['__experimental_passkeys']; /** * This cache is used to store the resources that Clerk fetches from the server when the network is offline. * * @experimental This API is experimental and may change at any moment. */ __experimental_resourceCache?: BuildClerkOptions['__experimental_resourceCache']; /** * Disables synchronization between the Clerk JS and native clients. * Only use this when the application does not render Clerk native components. * * @experimental This API is experimental and may change at any moment. */ __experimental_disableNativeClientSync?: boolean; }; const SDK_METADATA = { name: PACKAGE_NAME, version: PACKAGE_VERSION, }; export function ClerkProvider(props: ClerkProviderProps): JSX.Element { const { children, tokenCache, publishableKey, proxyUrl, domain, __experimental_passkeys, experimental, __experimental_resourceCache, __experimental_disableNativeClientSync = false, ...rest } = props; const pk = publishableKey; const nativeModule = isNative() && !__experimental_disableNativeClientSync ? ClerkExpoModule : null; const tokenCacheRef = useRef(tokenCache); tokenCacheRef.current = tokenCache; const nativeProxyUrl = typeof proxyUrl === 'string' && proxyUrl ? proxyUrl : null; const nativeClientSync = useMemo( () => (nativeModule ? createNativeClientSync(nativeModule, pk, nativeProxyUrl, () => tokenCacheRef.current) : null), [nativeModule, pk, nativeProxyUrl], ); const clerkInstance = isNative() ? getClerkInstance({ publishableKey: pk, tokenCache: nativeClientSync?.tokenCache ?? tokenCache, proxyUrl, domain, __experimental_passkeys, __experimental_resourceCache, }) : null; useEffect( () => nativeClientSync && clerkInstance ? nativeClientSync.attach(clerkInstance as unknown as SyncableClerk) : undefined, [nativeClientSync, clerkInstance], ); // Needed for `useOAuth` / `useSSO` to work correctly on web — must stay synchronous during render // so the redirect URL is caught before children mount. Resolves to a no-op on native via the // sibling `maybeCompleteAuthSession.ts`, which keeps Metro from statically bundling // `expo-web-browser` (an optional peer) for native consumers. if (isWeb()) { maybeCompleteAuthSession(); } return ( {children} ); }