-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.js
More file actions
147 lines (129 loc) · 3.55 KB
/
Copy pathApp.js
File metadata and controls
147 lines (129 loc) · 3.55 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import * as React from "react";
import {
View,
Text,
Alert,
StyleSheet,
Button,
TextInput,
TouchableWithoutFeedback,
Keyboard,
Dimensions,
KeyboardAvoidingView,
Platform,
TouchableOpacity,
StatusBar,
} from "react-native";
import { NavigationContainer } from "@react-navigation/native";
import LogInScreen from "./src/screens/LogInScreen";
import SignUpScreen from "./src/screens/SignUpScreen";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import AuthContextProvider, { AuthContext } from "./src/store/AuthContext";
import { useContext, useEffect, useState } from "react";
import AppLoading from "expo-app-loading";
import AsyncStorage from "@react-native-async-storage/async-storage";
import InstructorMainScreenTabs from "./src/screens/InstructorScreens/InstructorMainScreenTabs";
import CreateContent from "./src/screens/InstructorScreens/DashBoardStackNavigator/CreateContentScreen";
import StudentMainScreenTabs from "./src/screens/StudentScreens/StudentMainScreenTabs";
const Stack = createNativeStackNavigator();
function AuthStack() {
return (
<Stack.Navigator
screenOptions={{
headerStyle: { backgroundColor: "#26A8FF" },
headerTintColor: "white",
contentStyle: { backgroundColor: "white" },
}}
>
<Stack.Screen
name="LogIn"
component={LogInScreen}
options={{ title: "LogIn" }}
/>
<Stack.Screen
name="SignUp"
component={SignUpScreen}
options={{ title: "Sign Up" }}
/>
</Stack.Navigator>
);
}
function AuthenticatedStack() {
const authCtx = useContext(AuthContext);
if(authCtx.user_type == 'student')
{
return (
<Stack.Navigator>
<Stack.Screen
name="StudentMainScreen"
component={StudentMainScreenTabs}
options={{ headerShown: false }}
/>
</Stack.Navigator>
);
}
else if (authCtx.user_type == 'instructor')
{
return (
<Stack.Navigator>
<Stack.Screen
name="InstructorMainScreen"
component={InstructorMainScreenTabs}
options={{ headerShown: false }}
/>
</Stack.Navigator>
);
}
else {return (
<Stack.Navigator>
<Stack.Screen
name="InstructorMainScreen"
component={InstructorMainScreenTabs}
options={{ headerShown: false }}
/>
</Stack.Navigator>
);}
}
function Navigation() {
const authCtx = useContext(AuthContext);
//check isAuthenticated using authcontext
//if is authenticated, allow access to authenticated stack, otherwise, authstack
return (
<NavigationContainer>
{!authCtx.isAuthenticated && <AuthStack />}
{authCtx.isAuthenticated && <AuthenticatedStack />}
</NavigationContainer>
);
}
function Root() {
const [isTryingLogin, setIsTryingLogin] = useState(true);
const authCtx = useContext(AuthContext);
//the first thing to do inroot is to check for token from async
//if there is a token, then set authcontext to appropriate values
useEffect(() => {
async function fetchToken() {
const storedToken = await AsyncStorage.getItem("token");
if (storedToken) {
authCtx.authenticate(storedToken);
}
setIsTryingLogin(false);
}
fetchToken();
authCtx.logout();
}, []);
//the loading icon
if (isTryingLogin) {
return <AppLoading />;
}
return <Navigation />;
}
export default function App() {
return (
<>
<StatusBar style="light" />
<AuthContextProvider>
<Root />
</AuthContextProvider>
</>
);
}