-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
175 lines (157 loc) · 4.44 KB
/
App.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import React, { useState, useEffect } from "react";
import { ActivityIndicator, View, StyleSheet, Text, Image } from "react-native";
import Amplify, { Auth } from "aws-amplify";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import awsmobile from "./aws-exports";
import SignIn from "./src/screens/SignIn";
import SignUp from "./src/screens/SignUp";
import ConfirmSignUp from "./src/screens/ConfirmSignUp";
import Home from "./src/screens/Home";
import Landing from "./src/screens/Landing";
import ForgotPassword from "./src/screens/ForgotPassword";
import ResetPassword from "./src/screens/ResetPassword";
import "react-native-gesture-handler";
Amplify.configure(awsmobile);
const AuthenticationStack = createStackNavigator();
const AppStack = createStackNavigator();
const AuthenticationNavigator = (props) => {
return (
<AuthenticationStack.Navigator headerShown={false}>
<AuthenticationStack.Screen
name="SignIn"
options={{
title: "Sign In",
headerStyle: {
backgroundColor: "black",
},
headerTintColor: "#fff",
headerTitleStyle: {
fontWeight: "bold",
},
}}
>
{(screenProps) => (
<SignIn {...screenProps} updateAuthState={props.updateAuthState} />
)}
</AuthenticationStack.Screen>
<AuthenticationStack.Screen
name="SignUp"
component={SignUp}
options={{
title: "Sign Up",
headerStyle: {
backgroundColor: "black",
},
headerTintColor: "#fff",
headerTitleStyle: {
fontWeight: "bold",
},
}}
/>
<AuthenticationStack.Screen
name="ConfirmSignUp"
component={ConfirmSignUp}
options={{
title: "Confirm Sign Up",
headerStyle: {
backgroundColor: "black",
},
headerTintColor: "#fff",
}}
/>
<AuthenticationStack.Screen
name="ForgotPassword"
component={ForgotPassword}
options={{
title: "Forgot Password",
headerStyle: {
backgroundColor: "black",
},
headerTintColor: "#fff",
}}
></AuthenticationStack.Screen>
<AuthenticationStack.Screen
name="ResetPassword"
component={ResetPassword}
options={{
title: "Reset Password",
headerStyle: {
backgroundColor: "black",
},
headerTintColor: "#fff",
}}
></AuthenticationStack.Screen>
</AuthenticationStack.Navigator>
);
};
const AppNavigator = (props) => {
return (
<AppStack.Navigator
screenOptions={{
headerShown: false,
}}
>
<AppStack.Screen name="Home">
{(screenProps) => (
<Home {...screenProps} updateAuthState={props.updateAuthState} />
)}
</AppStack.Screen>
</AppStack.Navigator>
);
};
const Initializing = () => {
const [show, setShow] = useState(false);
useEffect(() => {
const timer = setTimeout(() => {
setShow(true);
}, 1000000);
return () => clearTimeout(timer);
}, []);
console.log("initializing...");
return (
<View
style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "black",
}}
>
{show ? <ActivityIndicator size="large" color="red" /> : <Landing />}
</View>
);
};
function App({ username }) {
const [isUserLoggedIn, setUserLoggedIn] = useState("initializing");
useEffect(() => {
checkAuthState();
}, []);
async function checkAuthState() {
try {
await Auth.currentAuthenticatedUser();
console.log("✅ User is signed in");
setUserLoggedIn("loggedIn");
} catch (err) {
console.log("❌ User is not signed in");
setUserLoggedIn("loggedOut");
}
}
function updateAuthState(isUserLoggedIn) {
setUserLoggedIn(isUserLoggedIn);
}
return (
<NavigationContainer>
{isUserLoggedIn === "initializing" && <Initializing />}
{isUserLoggedIn === "loggedIn" && (
<AppNavigator updateAuthState={updateAuthState} />
)}
{isUserLoggedIn === "loggedOut" && (
<AuthenticationNavigator updateAuthState={updateAuthState} />
)}
</NavigationContainer>
);
}
const styles = StyleSheet.create({
});
export default App;