Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/kadi-env-var-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"@clerk/expo": major
---

The `publishableKey` prop is now required in `ClerkProvider`. Previously, the prop was optional and would fall back to `EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY` or `CLERK_PUBLISHABLE_KEY` environment variables.

Environment variables inside `node_modules` are not inlined during production builds in React Native/Expo, which could cause apps to crash in production when the publishable key is undefined.

You must now explicitly pass the `publishableKey` prop to `ClerkProvider`:

```tsx
const publishableKey = process.env.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY!;

<ClerkProvider publishableKey={publishableKey}>
{/* Your app */}
</ClerkProvider>
Comment on lines +11 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Add validation to prevent undefined publishableKey.

The migration example uses the non-null assertion operator (!) without validation. If EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY is not set, the app will crash—exactly the issue this PR aims to prevent.

✅ Proposed fix with proper error handling
-const publishableKey = process.env.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY!;
+const publishableKey = process.env.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY;
+
+if (!publishableKey) {
+  throw new Error('Missing Publishable Key. Please set EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY in your .env file.');
+}

 <ClerkProvider publishableKey={publishableKey}>
   {/* Your app */}
 </ClerkProvider>
🤖 Prompt for AI Agents
In @.changeset/kadi-env-var-fix.md around lines 11 - 16, Replace the unchecked
non-null assertion on process.env.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY by
validating it before use: read process.env.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY
into publishableKey, check that publishableKey is defined/non-empty and if not
either throw a clear error (with context) or provide a safe fallback, and only
then pass publishableKey into <ClerkProvider> so the app won't crash at runtime;
locate the usage by the publishableKey variable and the <ClerkProvider>
component to apply the check.

15 changes: 11 additions & 4 deletions packages/expo/src/provider/ClerkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ export type ClerkProviderProps<TUi extends Ui = Ui> = Omit<
'publishableKey'
> & {
/**
* Used to override the default EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY env variable if needed.
* This is optional for Expo as the ClerkProvider will automatically use the EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY env variable if it exists.
* 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!;
* <ClerkProvider publishableKey={publishableKey}>
* ```
*/
publishableKey?: string;
publishableKey: string;
/**
* The token cache is used to persist the active user's session token. Clerk stores this token in memory by default, however it is recommended to use a token cache for production applications.
* @see https://clerk.com/docs/quickstarts/expo#configure-the-token-cache-with-expo
Expand Down Expand Up @@ -53,7 +60,7 @@ export function ClerkProvider<TUi extends Ui = Ui>(props: ClerkProviderProps<TUi
__experimental_resourceCache,
...rest
} = props;
const pk = publishableKey || process.env.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY || process.env.CLERK_PUBLISHABLE_KEY || '';
const pk = publishableKey;

if (isWeb()) {
// This is needed in order for useOAuth to work correctly on web.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let __internal_clerk: HeadlessBrowserClerk | BrowserClerk | undefined;
export function createClerkInstance(ClerkClass: typeof Clerk) {
return (options?: BuildClerkOptions): HeadlessBrowserClerk | BrowserClerk => {
const {
publishableKey = process.env.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY || process.env.CLERK_PUBLISHABLE_KEY || '',
publishableKey = '',
tokenCache = MemoryTokenCache,
__experimental_resourceCache: createResourceCache,
} = options || {};
Expand Down
Loading