Ok it might work
This commit is contained in:
@@ -1,29 +1,32 @@
|
||||
import { BirthdaysProvider } from "@/context/birthdays-context";
|
||||
import { useAuth } from "@/context/auth-context";
|
||||
import { Stack, router, useRootNavigationState, useSegments } from "expo-router";
|
||||
import { useEffect } from "react";
|
||||
import React, { useEffect } from "react";
|
||||
import LoadingScreen from "./loading-screen";
|
||||
|
||||
export default function RootLayout() {
|
||||
const { user, isHydrated } = useAuth();
|
||||
const navigationState = useRootNavigationState();
|
||||
const segments = useSegments();
|
||||
const [loaded, setLoaded] = React.useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isHydrated || !navigationState?.key) {
|
||||
return;
|
||||
}
|
||||
if (!isHydrated || !navigationState?.key) return;
|
||||
|
||||
const inAuthGroup = segments[0] === "(auth)";
|
||||
const inAuthGroup = segments.length === 0;
|
||||
|
||||
if (!user && !inAuthGroup) {
|
||||
router.replace("/(auth)/login");
|
||||
return;
|
||||
}
|
||||
if (!user) {
|
||||
router.replace("/(auth)/login");
|
||||
} else if (inAuthGroup) {
|
||||
router.replace("/(tabs)");
|
||||
}
|
||||
|
||||
if (user && inAuthGroup) {
|
||||
router.replace("/(tabs)");
|
||||
}
|
||||
}, [isHydrated, navigationState?.key, segments, user]);
|
||||
setLoaded(true); // 👈 ALWAYS run this
|
||||
}, [isHydrated, navigationState?.key, segments, user]);
|
||||
|
||||
if(!loaded) {
|
||||
return (<LoadingScreen />); // or a loading spinner
|
||||
}
|
||||
|
||||
return (
|
||||
<BirthdaysProvider>
|
||||
|
||||
Reference in New Issue
Block a user