-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
75 lines (66 loc) · 2.15 KB
/
App.tsx
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
import React, {useState, useEffect, useCallback} from 'react';
import {NavigationContainer} from '@react-navigation/native';
import auth from '@react-native-firebase/auth';
import firestore from '@react-native-firebase/firestore';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {MainBottomTabNavigator} from './src/navigations/BottomTabNavigator';
import {Login} from './src/screens/Login';
export default function App() {
// Set an initializing state whilst Firebase connects
const [initializing, setInitializing] = useState(true);
const [user, setUser] = useState(null);
// Handle user state changes
const onAuthStateChanged = useCallback(
async (authUser) => {
if (authUser) {
try {
const fcm = await AsyncStorage.getItem('fcm');
const parsedFcm = fcm != null ? JSON.parse(fcm) : null;
if (parsedFcm) {
// update fcm in firebase
const users = firestore().collection('users');
const userDocRef = users.doc(authUser.uid);
const doc = await userDocRef.get();
let devices = [];
if (doc.data()?.devices) {
devices = doc.data()?.devices;
}
const tokenExists = devices.find(
(dev: any) => dev.pushToken === parsedFcm.token,
);
if (!tokenExists) {
devices.push({os: parsedFcm.os, pushToken: parsedFcm.token});
await users
.doc(authUser.uid)
.set({devices, uid: authUser.uid}, {merge: true});
}
}
} catch (e) {
console.log(e);
}
setUser(authUser);
} else {
setUser(null);
}
if (initializing) {
setInitializing(false);
}
},
[initializing],
);
useEffect(() => {
const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
return subscriber; // unsubscribe on unmount
}, [onAuthStateChanged]);
if (initializing) {
return null;
}
if (!user) {
return <Login />;
}
return (
<NavigationContainer>
<MainBottomTabNavigator />
</NavigationContainer>
);
}