-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
296 lines (282 loc) · 9.14 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { View, StyleSheet, Dimensions } from "react-native";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import React, { useState, useEffect } from "react";
import MainTabNavigator from "./screens/MainTabNavigator";
import AuthStack from "./screens/authentication-screens/AuthStack";
import firebase from "./database/firebaseDB";
import { ActivityIndicator } from "react-native";
import { AuthContext, authContext } from "./assets/AuthContext";
import { LogBox } from "react-native";
import { SafeAreaProvider, SafeAreaView } from "react-native-safe-area-context";
LogBox.ignoreLogs(["Setting a timer"]);
LogBox.ignoreAllLogs();
const Stack = createStackNavigator();
export default function App() {
const [loading, setLoading] = useState(true);
const [user, setUser] = useState(null);
const [isValidated, setValidated] = useState(false);
/* Monitors authentication state for persistent log in.
On initial loading of the app, if user is previously logged in, this
function will get the user data */
useEffect(() => {
console.log("Getting user data on startup");
const usersRef = firebase.firestore().collection("users");
firebase.auth().onAuthStateChanged((user) => {
if (user) {
usersRef
.doc(user.uid)
.get()
.then((document) => {
const userData = document.data();
setLoading(false);
setUser(userData);
})
.catch((error) => {
setLoading(false);
});
console.log(`Email isValidated?: ${user.emailVerified}`);
setValidated(user.emailVerified);
// Add uid to logs on log in (TEMPORARY)
usersRef
.doc(user.uid)
.collection("logs")
.doc("logsDoc")
.set({ id: user.uid }, { merge: true });
} else {
setLoading(false);
setUser(null);
}
});
}, []);
const authContext = {
terminateAccount: (data) => {
const usersRef = firebase.firestore().collection("users");
// Remove user data
usersRef
.doc(data.uid)
.delete()
.then(() => {
console.log(`Document ${data.uid} successfully deleted!`);
// Remove auth for user
data.user
.delete()
.then(() => {
setUser(null);
setValidated(false);
console.log(`User ${data.uid} account terminated!`);
})
.catch((error) => {
console.log("Error terminating account: ", error);
});
})
.catch((error) => {
console.error("Error removing document: ", error);
});
},
signUpSuccess: () => {
if (!isValidated) {
console.log("sign up success");
setValidated(true);
}
},
forgotPassword: async (data) => {
firebase
.auth()
.sendPasswordResetEmail(data.email)
.then(() => {
data.setSubmit(true);
data.setStatusMsg("Password reset email sent!");
console.log("Password reset email sent!");
})
.catch((error) => {
console.log(error);
if (error.code === "auth/user-not-found") {
data.setErrorMsg(
"This email does not have an account yet! Create one?"
);
}
});
},
logIn: async (data) => {
firebase
.auth()
.signInWithEmailAndPassword(data.email, data.password)
.then((response) => {
const uid = response.user.uid;
const usersRef = firebase.firestore().collection("users");
usersRef
.doc(uid)
.get()
.then((firestoreDocument) => {
if (!firestoreDocument.exists) {
console.log("User's document does not exist anymore.");
return;
} else if (response.user.emailVerified == false) {
// Checks if the user has been verified
setValidated(false);
response.user
.sendEmailVerification()
.then(() => {
console.log(
`Email verification re-sent to ${response.user.email}`
);
})
.catch((error) => {
console.log(error);
});
data.setErrorMsg(
"Please check your inbox and verify your email before trying again or sign up again with your personal email!"
);
} else {
const userData = firestoreDocument.data();
setUser(userData);
setValidated(true);
console.log("User is logged in!");
}
})
.catch((error) => {
console.log(error);
});
})
.catch((error) => {
if (error.code === "auth/wrong-password") {
data.setErrorMsg("The password entered is incorrrect!");
}
if (error.code === "auth/too-many-requests") {
data.setErrorMsg(
"Too many failed login attempts. Please try again later or reset your password now."
);
}
if (error.code === "auth/user-not-found") {
data.setErrorMsg(
"There is no existing account for this email. Sign up for a new account?"
);
}
});
},
logOut: async () => {
firebase
.auth()
.signOut()
.then(() => {
setUser(null);
console.log("User is logged out!");
})
.catch((error) => {
console.log(error);
});
},
signUp: async (data) => {
// console.log(data);
// Create user profile on firebase authentication
firebase
.auth()
.createUserWithEmailAndPassword(data.email, data.password)
.then((cred) => {
const uid = cred.user.uid;
// Add user's profile information in firestore
firebase.firestore().collection("users").doc(uid).set({
id: uid,
email: data.email,
firstName: data.firstName,
lastName: data.lastName,
gender: data.gender,
dateOfBirth: data.dateOfBirth,
faculty: data.faculty,
byo: 0,
coin: 0,
containerDate: [],
containerReturned: 0,
cupDate: [],
cupReturned: 0,
location: "",
numCup: 0,
numContainer: 0,
});
// Add logs collection to each user
firebase
.firestore()
.collection("users")
.doc(uid)
.collection("logs")
.doc("logsDoc")
.set({ logsArray: [], id: uid });
// Set total users count to increment by 1 for each sign in
firebase
.firestore()
.collection("overall")
.doc("overallStats")
.update({
totalUsers: firebase.firestore.FieldValue.increment(1),
});
// Send email verification to user
cred.user
.sendEmailVerification()
.then(() => {
console.log(`Email verification sent to ${cred.user.email}`);
console.log(
`Initial email verification state: ${cred.user.emailVerified}`
);
})
.catch((error) => {
console.log(error);
data.setErrorMsg(
"Error sending out verification email, are you sure this email is valid?"
);
});
})
.then(() => {
console.log("User Account created!");
data.setSubmit(true);
})
.catch((error) => {
console.log(error);
if (error.code === "auth/email-already-in-use") {
data.setErrorMsg("This email address is already in use!");
}
});
},
};
if (loading) {
return (
<View style={{ justifyContent: "center", alignItem: "center" }}>
<ActivityIndicator size="large" />
</View>
);
}
return (
<SafeAreaProvider>
<AuthContext.Provider value={authContext}>
<NavigationContainer>
{console.log(
`Authentication screen check isValidated? : ${isValidated}`
)}
{user && isValidated ? (
<Stack.Navigator>
<Stack.Screen
name="Main Tab Navigator"
component={MainTabNavigator}
options={{
headerShown: false,
}}
initialParams={user}
/>
</Stack.Navigator>
) : (
<Stack.Navigator>
<Stack.Screen
name="Auth Stack"
component={AuthStack}
options={{
animationTypeForReplace: "pop",
headerShown: false,
}}
/>
</Stack.Navigator>
)}
</NavigationContainer>
</AuthContext.Provider>
</SafeAreaProvider>
);
}