-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
131 lines (115 loc) · 4.27 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
import "react-native-gesture-handler";
import AppNavigation from "./Navigation";
import SignInScreen from "./screens/SignInScreen";
import * as Google from "expo-auth-session/providers/google";
import * as WebBrowser from "expo-web-browser";
import * as React from "react";
import * as Location from "expo-location";
import {
GoogleAuthProvider,
onAuthStateChanged,
signInWithCredential,
} from "firebase/auth";
import { useFonts } from "expo-font";
import { auth, db, usersRef } from "./firebaseConfig";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { ActivityIndicator, View } from "react-native";
import { UserLocationContext } from "./context/UserLocationContext";
import { addDoc, query, where, getDocs, collection } from "firebase/firestore";
WebBrowser.maybeCompleteAuthSession();
function App() {
const [userInfo, setUserInfo] = React.useState();
const [loading, setLoading] = React.useState(false);
const [location, setLocation] = React.useState(null);
const [errorMsg, setErrorMsg] = React.useState(null);
const [request, response, promptAsync] = Google.useIdTokenAuthRequest({
iosClientId:
"351964922129-aaj7t8allpeonrv3u5copigra17s8bvo.apps.googleusercontent.com",
androidClientId:
"351964922129-cc2cjhidif7tqmle25kei6ok8nutjodi.apps.googleusercontent.com",
});
const [fontsLoaded] = useFonts({
"poppins-light": require("./assets/fonts/poppins/Poppins-Light.ttf"),
"poppins-regular": require("./assets/fonts/poppins/Poppins-Regular.ttf"),
"poppins-medium": require("./assets/fonts/poppins/Poppins-Medium.ttf"),
"poppins-semibold": require("./assets/fonts/poppins/Poppins-SemiBold.ttf"),
"familjen-regular": require("./assets/fonts/familjen/FamiljenGrotesk-Regular.ttf"),
"familjen-medium": require("./assets/fonts/familjen/FamiljenGrotesk-Medium.ttf"),
"familjen-semibold": require("./assets/fonts/familjen/FamiljenGrotesk-SemiBold.ttf"),
});
const checkLocalUser = async () => {
try {
setLoading(true);
const userJSON = await AsyncStorage.getItem("@user");
const userData = userJSON ? JSON.parse(userJSON) : null;
setUserInfo(userData);
} catch (error) {
alert(error.message);
} finally {
setLoading(false);
}
};
React.useEffect(() => {
if (response?.type == "success") {
const { id_token } = response.params;
const credential = GoogleAuthProvider.credential(id_token);
signInWithCredential(auth, credential);
}
}, [response]);
React.useEffect(() => {
checkLocalUser();
const unsub = onAuthStateChanged(auth, async (user) => {
if (user) {
await AsyncStorage.setItem("@user", JSON.stringify(user));
setUserInfo(user);
// Check if the user already exists in Firestore
const usersRef = collection(db, "users");
const q = query(usersRef, where("userId", "==", user.uid));
const querySnapshot = await getDocs(q);
if (querySnapshot.size === 0) {
// User does not exist, add them to Firestore
await addDoc(usersRef, {
displayName: user.displayName,
photoURL: user.photoURL,
email: user.email,
userId: user.uid,
coverPhoto: "",
bio: "",
});
console.log("New User => ", user.displayName, user.uid);
} else {
console.log("User already exists:", user.displayName, user.uid);
}
} else {
console.log("User not authenticated");
}
});
return () => unsub();
}, []);
React.useEffect(() => {
(async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== "granted") {
setErrorMsg("Permission to access location was denied");
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
})();
}, []);
if (loading) {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<ActivityIndicator size={"large"} />
</View>
);
}
return userInfo ? (
<UserLocationContext.Provider value={{ location, setLocation }}>
<AppNavigation />
</UserLocationContext.Provider>
) : (
<SignInScreen promptAsync={promptAsync} />
);
}
export default App;