Skip to content
Source
Sign inSign up
Open History to see the last commit

CloudAuthProvider.tsx

32 lines1.2 KB

1import { ClerkProvider, getClerkInstance } from "@clerk/expo";
2import { tokenCache } from "@clerk/expo/token-cache";
3import type { ReactNode } from "react";
4​
5import { cloudAccountService, deactivateCloudRelayAccount } from "./cloudAccountService";
6import { resolveCloudPublicConfig } from "./publicConfig";
7​
8const cloudConfig = resolveCloudPublicConfig();
9// Build-time constant: null when this build has no cloud config.
10const publishableKey = cloudConfig.relay.url ? cloudConfig.clerk.publishableKey : null;
11​
12if (publishableKey) {
13 // The same native singleton ClerkProvider renders with, so the service
14 // observes it for the app's lifetime without a React subscription.
15 cloudAccountService.attach(getClerkInstance({ publishableKey }));
16} else {
17 // Without cloud config no account can ever activate, so drop anything a
18 // previous build with cloud enabled left registered.
19 deactivateCloudRelayAccount();
21​
22export function CloudAuthProvider(props: { readonly children: ReactNode }) {
23 if (!publishableKey) {
24 return props.children;
25 }
26​
27 return (
28 <ClerkProvider publishableKey={publishableKey} tokenCache={tokenCache}>
29 {props.children}
30 </ClerkProvider>
31 );