-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthLoading.js
More file actions
80 lines (68 loc) · 2.58 KB
/
AuthLoading.js
File metadata and controls
80 lines (68 loc) · 2.58 KB
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
import React, {useEffect} from "react"
import {View, Text, ActivityIndicator} from "react-native"
import { createStackNavigator } from '@react-navigation/stack'
import { useDispatch, useSelector } from 'react-redux';
import {login, logout} from "./src/redux/slices"
import Chat from "./src/component/Chat";
import { LoginStack, MainStack } from "./AppNav";
import auth from '@react-native-firebase/auth';
import {KeyboardAvoidingView} from "react-native"
import messaging from '@react-native-firebase/messaging';
async function requestUserPermission() {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
console.log('Authorization status:', authStatus);
let token = await messaging().getToken();
console.log(`FCM Token: ${token}`);
let result = await fetch("http://www.kodku.com:3000/phone/", {
method: 'POST',
body: JSON.stringify({token: token}),
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
}
}
const AuthStackNav = createStackNavigator();
const AuthLoading = (props) => {
let dispatch = useDispatch();
let authenticated = useSelector(state => state.authenticated);
useEffect(() => {
requestUserPermission();
const subscriber = auth().onAuthStateChanged(user => {
console.log("Auth changed");
console.log(user);
if (user) {
dispatch(login())
} else {
dispatch(logout())
}
})
return subscriber;
}, []);
return (
<>
{authenticated == undefined &&
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<ActivityIndicator size={'large'} />
</View>
}
{authenticated != undefined &&
<KeyboardAvoidingView style={{flex: 1}} behavior="padding" enabled>
<AuthStackNav.Navigator screenOptions={{headerShown: false}}>
{authenticated ?
<AuthStackNav.Screen name="Main" component={MainStack} />
:
<AuthStackNav.Screen name="Login" component={LoginStack} />
}
</AuthStackNav.Navigator>
</KeyboardAvoidingView>
}
</>
)
}
export default AuthLoading;