import { ClerkProvider, ClerkLoaded } from "@clerk/clerk-expo";
import { Stack } from "expo-router";
import * as SecureStore from "expo-secure-store";
import { View, Text } from "react-native";
import { useEffect, useState } from "react";
import { SafeAreaProvider } from "react-native-safe-area-context";
import { validateEnv } from "../utils/env";
import { ThemeProvider } from "../contexts/ThemeContext";
import { StatisticsProvider } from "../contexts/StatisticsContext";
import { FitnessGoalsProvider } from "../contexts/FitnessGoalsContext";
import { RecommendationsProvider } from "../contexts/RecommendationsContext";
import { NotificationsProvider } from "../contexts/NotificationsContext";
import log from "../utils/logger";
// Wrapper to use notification permissions hook after ClerkLoaded
function AppContent() {
// Import here to avoid hook execution before Clerk is loaded
const {
useNotificationPermissions,
} = require("../hooks/useNotificationPermissions");
useNotificationPermissions();
return (
);
}
// Validate environment variables on app startup
try {
const env = validateEnv();
log.info("Environment validation passed", {
appName: env.EXPO_PUBLIC_APP_NAME,
apiUrl: env.EXPO_PUBLIC_API_URL,
});
} catch (error) {
log.error("Environment validation failed", error);
// In development, show the error but allow app to continue
// In production, this will crash the app (as it should)
if (!__DEV__) {
throw error;
}
}
// Token cache for Clerk
const tokenCache = {
async getToken(key: string) {
try {
const value = await SecureStore.getItemAsync(key);
log.debug("Getting token from cache", {
key,
exists: !!value,
preview:
value && value.length > 50
? value.substring(0, 50) + "..."
: undefined,
});
return value;
} catch (err) {
log.error("Failed to get token from cache", err, { key });
return null;
}
},
async saveToken(key: string, value: string) {
try {
log.debug("Saving token to cache", {
key,
preview:
value && value.length > 50
? value.substring(0, 50) + "..."
: undefined,
});
return SecureStore.setItemAsync(key, value);
} catch (err) {
log.error("Failed to save token to cache", err, { key });
}
},
};
export default function RootLayout() {
const publishableKey = process.env.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY;
const [cacheCleared, setCacheCleared] = useState(false);
log.debug("RootLayout rendering", {
hasPublishableKey: !!publishableKey,
});
// TEMPORARY: Clear Clerk cache on app start to fix instance mismatch
useEffect(() => {
log.debug("Starting cache cleanup");
const clearOldClerkCache = async () => {
try {
log.debug("Checking for old Clerk tokens");
const keysToCheck = [
"__clerk_client_jwt",
"__clerk_db_jwt",
"__clerk_client_uat",
"__clerk_session_id",
"__clerk_refresh_token",
"__clerk_session_jwt",
];
for (const key of keysToCheck) {
try {
const value = await SecureStore.getItemAsync(key);
if (value) {
log.debug("Found token", {
key,
preview: value.substring(0, 80) + "...",
});
// Check if it's from the old instance
if (value.includes("pleasing-pheasant-20")) {
log.debug("Deleting old token", { key });
await SecureStore.deleteItemAsync(key);
} else if (value.includes("needed-elephant-64")) {
log.debug("Token is from correct instance");
} else {
log.warn("Token doesn't match known instances", { key });
}
}
} catch (e) {
log.error("Error checking token key", e, { key });
}
}
log.info("Old token cleanup complete");
setCacheCleared(true);
} catch (error) {
log.error("Error clearing old cache", error);
setCacheCleared(true); // Continue anyway
}
};
clearOldClerkCache();
}, []);
if (!publishableKey) {
log.error("No Clerk publishable key found");
return (
Missing Clerk Publishable Key
Please add EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY to your .env file
);
}
if (!cacheCleared) {
log.debug("Waiting for cache to clear");
return (
Clearing old authentication cache...
Check the terminal for logs...
);
}
log.debug("Rendering ClerkProvider", {
keyPreview: publishableKey?.substring(0, 20) + "...",
});
return (
);
}