FAIRE ATTENTION A FIREBASE_CONFIG.JS
This commit is contained in:
@@ -1,27 +1,73 @@
|
||||
import { DarkTheme, DefaultTheme, ThemeProvider } from '@react-navigation/native';
|
||||
import { Stack } from 'expo-router';
|
||||
import { StatusBar } from 'expo-status-bar';
|
||||
import 'react-native-reanimated';
|
||||
|
||||
import { useColorScheme } from '@/hooks/use-color-scheme';
|
||||
import {
|
||||
DarkTheme,
|
||||
DefaultTheme,
|
||||
ThemeProvider,
|
||||
} from "@react-navigation/native";
|
||||
import { Stack } from "expo-router";
|
||||
import { StatusBar } from "expo-status-bar";
|
||||
import "react-native-reanimated";
|
||||
import { useEffect, useState } from "react";
|
||||
import { onAuthStateChanged, User } from "firebase/auth";
|
||||
import { auth, db } from "../firebase_config";
|
||||
import { useRouter } from "expo-router";
|
||||
import { useColorScheme } from "@/hooks/use-color-scheme";
|
||||
import { doc, getDoc } from "firebase/firestore";
|
||||
|
||||
export const unstable_settings = {
|
||||
anchor: '(tabs)',
|
||||
anchor: "(tabs)",
|
||||
};
|
||||
|
||||
export default function RootLayout() {
|
||||
const colorScheme = useColorScheme();
|
||||
const router = useRouter();
|
||||
const [user, setUser] = useState<User | null>(null);
|
||||
const [userRole, setUserRole] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const unsubscribe = onAuthStateChanged(auth, async (currentUser) => {
|
||||
setUser(currentUser);
|
||||
if (!currentUser) {
|
||||
router.replace("/login/login");
|
||||
return;
|
||||
}
|
||||
|
||||
const userDocRef = doc(db, "user", currentUser.uid);
|
||||
const userDoc = await getDoc(userDocRef);
|
||||
|
||||
if (!userDoc.exists()) {
|
||||
router.replace("/login/login");
|
||||
return;
|
||||
}
|
||||
|
||||
const { role } = userDoc.data();
|
||||
setUser(currentUser);
|
||||
setUserRole(role);
|
||||
|
||||
if (role === "chef") {
|
||||
router.replace("/(tabs)");
|
||||
} else if (role === "responsable") {
|
||||
router.replace("/login/login");
|
||||
} else {
|
||||
router.replace("/login/login");
|
||||
}
|
||||
});
|
||||
|
||||
return unsubscribe;
|
||||
}, []);
|
||||
|
||||
return (
|
||||
|
||||
<ThemeProvider value={colorScheme === 'dark' ? DarkTheme : DefaultTheme}>
|
||||
<ThemeProvider value={colorScheme === "dark" ? DarkTheme : DefaultTheme}>
|
||||
<Stack>
|
||||
<Stack.Screen name="(tabs)" options={{ headerShown: false }} />
|
||||
<Stack.Screen name="selectChantier" options={{ headerShown: false }}/>
|
||||
<Stack.Screen name="modal" options={{ presentation: 'modal', title: 'Modal' }} />
|
||||
<Stack.Screen
|
||||
name="modal"
|
||||
options={{ presentation: "modal", title: "Modal" }}
|
||||
/>
|
||||
<Stack.Screen name="login" options={{ headerShown: false }} />
|
||||
</Stack>
|
||||
<StatusBar style="auto" />
|
||||
</ThemeProvider>
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user