-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
60 lines (53 loc) · 1.65 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
import React, {useEffect, useState} from 'react';
import SplashScreen from 'react-native-splash-screen';
import 'react-native-gesture-handler';
import {SafeAreaProvider} from 'react-native-safe-area-context';
import auth from '@react-native-firebase/auth';
import {useStore} from './src/store/store';
import AppRoutes from './src/navigators/AppRoutes';
const App = () => {
// state
const [userLogin, setUserLogin] = useState(false);
const [loading, setLoading] = useState(true);
// store
const UserDetail = useStore((state: any) => state.UserDetail);
const setUserDetail = useStore((state: any) => state.setUserDetail);
// use Effect to show Splash screen
useEffect(() => {
SplashScreen.hide();
}, []);
// Signing in Anonymously user
useEffect(() => {
const subscriber = auth().onAuthStateChanged(async user => {
console.log('no user found');
console.log(user);
if (user) {
await setUserDetail(user);
setUserLogin(true);
} else {
// Sign in anonymously if no user is found
console.log('No user found, signing in anonymously...');
auth()
.signInAnonymously()
.then(async userCredential => {
await setUserDetail(userCredential.user);
setUserLogin(true);
})
.catch(error => {
console.error('Error signing in anonymously:', error);
});
}
if (loading) setLoading(false);
});
return () => subscriber();
}, []);
if (loading) {
return null;
}
return (
<SafeAreaProvider>
<AppRoutes isUserLogin={userLogin}></AppRoutes>
</SafeAreaProvider>
);
};
export default App;