Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
61 lines (47 loc) · 1.52 KB
/
Copy pathApp.js
File metadata and controls
61 lines (47 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { useContext, useEffect, useState } from "react";
import { NavigationContainer } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import { StatusBar } from "expo-status-bar";
import AsyncStorage from "@react-native-async-storage/async-storage";
import AuthContextProvider, { AuthContext } from "./store/auth-context";
import AuthStack from "./screens/Navigation/AuthStack";
import AuthenticatedStack from "./screens/Navigation/AuthenticatedStack";
import LoadingOverlay from "./components/ui/LoadingOverlay";
const Stack = createNativeStackNavigator();
function Navigation() {
const authCtx = useContext(AuthContext);
return (
<NavigationContainer>
{!authCtx.isAuthenticated && <AuthStack stack={Stack} />}
{authCtx.isAuthenticated && <AuthenticatedStack stack={Stack} />}
</NavigationContainer>
);
}
function Root() {
const [isTryingLogin, setIsTryingLogin] = useState(true);
const authCtx = useContext(AuthContext);
useEffect(() => {
async function fetchToken() {
const storedToken = await AsyncStorage.getItem("token");
if (storedToken) {
authCtx.authenticate(storedToken);
}
setIsTryingLogin(false);
}
fetchToken();
}, []);
if (isTryingLogin) {
return <LoadingOverlay />
}
return <Navigation />;
}
export default function App() {
return (
<>
<StatusBar style="light" />
<AuthContextProvider>
<Root />
</AuthContextProvider>
</>
);
}