Fix auth routing issue
parent
eeb6683b59
commit
ad7d59b7a5
102
app/_layout.tsx
102
app/_layout.tsx
|
|
@ -1,77 +1,61 @@
|
||||||
import i18next, { getLanguage } from "../services/i18n/index";
|
// app/_layout.tsx
|
||||||
import { Stack, useRouter } from "expo-router";
|
import { Stack } from "expo-router";
|
||||||
import * as SplashScreen from "expo-splash-screen";
|
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import "react-native-reanimated";
|
import { Provider, useDispatch, useSelector } from "react-redux";
|
||||||
import { I18nextProvider } from "react-i18next";
|
import { store, AppDispatch, RootState } from "@/store";
|
||||||
import { Provider } from "react-redux";
|
|
||||||
import { store } from "@/store";
|
|
||||||
import { PaperProvider } from "react-native-paper";
|
import { PaperProvider } from "react-native-paper";
|
||||||
|
import { I18nextProvider } from "react-i18next";
|
||||||
|
import i18next from "@/services/i18n";
|
||||||
import SnackbarProvider from "@/contexts/Snackbar";
|
import SnackbarProvider from "@/contexts/Snackbar";
|
||||||
|
import * as SplashScreen from "expo-splash-screen";
|
||||||
export { ErrorBoundary } from "expo-router";
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||||
|
import { STORAGE_KEYS } from "@/constants/config";
|
||||||
export const unstable_settings = {
|
import { setIsLoggedIn } from "@/store/authSlice";
|
||||||
initialRouteName: "(tabs)",
|
|
||||||
};
|
|
||||||
|
|
||||||
SplashScreen.preventAutoHideAsync();
|
SplashScreen.preventAutoHideAsync();
|
||||||
|
|
||||||
export default function RootLayout() {
|
export default function RootLayout() {
|
||||||
const [appIsReady, setAppIsReady] = useState(false);
|
|
||||||
const [shouldRedirect, setShouldRedirect] = useState(false);
|
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const initLang = async () => {
|
|
||||||
const lang = await getLanguage();
|
|
||||||
if (!lang) {
|
|
||||||
setShouldRedirect(true);
|
|
||||||
}
|
|
||||||
setAppIsReady(true);
|
|
||||||
};
|
|
||||||
|
|
||||||
initLang();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (appIsReady) {
|
|
||||||
SplashScreen.hideAsync();
|
|
||||||
router.replace("/init/language");
|
|
||||||
}
|
|
||||||
}, [appIsReady]);
|
|
||||||
|
|
||||||
// useEffect(() => {
|
|
||||||
// if (appIsReady && shouldRedirect) {
|
|
||||||
// router.replace("/init/language");
|
|
||||||
// setShouldRedirect(false);
|
|
||||||
// }
|
|
||||||
// }, [appIsReady, shouldRedirect]);
|
|
||||||
|
|
||||||
if (!appIsReady) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return <RootLayoutNav />;
|
|
||||||
}
|
|
||||||
|
|
||||||
function RootLayoutNav() {
|
|
||||||
return (
|
return (
|
||||||
<PaperProvider>
|
<PaperProvider>
|
||||||
<SnackbarProvider>
|
<SnackbarProvider>
|
||||||
<Provider store={store}>
|
<Provider store={store}>
|
||||||
<I18nextProvider i18n={i18next}>
|
<I18nextProvider i18n={i18next}>
|
||||||
<Stack
|
<SplashAndAuthRouter />
|
||||||
screenOptions={{
|
|
||||||
headerShown: false,
|
|
||||||
animation: "fade",
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Stack.Screen name="(tabs)" options={{ headerShown: false }} />
|
|
||||||
</Stack>
|
|
||||||
</I18nextProvider>
|
</I18nextProvider>
|
||||||
</Provider>
|
</Provider>
|
||||||
</SnackbarProvider>
|
</SnackbarProvider>
|
||||||
</PaperProvider>
|
</PaperProvider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function SplashAndAuthRouter() {
|
||||||
|
const dispatch = useDispatch<AppDispatch>();
|
||||||
|
const isLoggedIn = useSelector((state: RootState) => state.auth.isLoggedIn);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const loadAuth = async () => {
|
||||||
|
const token = await AsyncStorage.getItem(STORAGE_KEYS.AUTH_TOKEN);
|
||||||
|
dispatch(setIsLoggedIn(!!token));
|
||||||
|
setLoading(false);
|
||||||
|
SplashScreen.hideAsync();
|
||||||
|
};
|
||||||
|
loadAuth();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (loading) return null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Stack screenOptions={{ headerShown: false }}>
|
||||||
|
<Stack.Protected guard={isLoggedIn}>
|
||||||
|
<Stack.Screen name="(tabs)" />
|
||||||
|
<Stack.Screen name="user" />
|
||||||
|
</Stack.Protected>
|
||||||
|
|
||||||
|
<Stack.Protected guard={!isLoggedIn}>
|
||||||
|
<Stack.Screen name="init/language" />
|
||||||
|
<Stack.Screen name="auth" />
|
||||||
|
</Stack.Protected>
|
||||||
|
</Stack>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue