-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
81 lines (68 loc) · 2.21 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
import React, { useState, useEffect } from 'react';
import 'react-native-gesture-handler';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import AsyncStorage from '@react-native-async-storage/async-storage';
import firebase from './firebase/fire';
import AfterLogin from './view/AfterLogin';
import Rotas from './Rotas';
import SignInScreen, { signInAsync } from './view/SignIn';
import { StatusBar } from 'expo-status-bar';
const Stack = createStackNavigator();
const GithubStorageKey = '@Expo:GithubToken';
const AfterLog = '@Expo:AfterLog'; //lolz whatever you want.
async function attemptToRestoreAuthAsync() {
let token = await AsyncStorage.getItem(GithubStorageKey);
if (token) {
return signInAsync(token);
}
}
async function logonAsync(setAfterLogon) {
let logon = await AsyncStorage.getItem(AfterLog);
if (logon) {
setAfterLogon(true);
} else {
setAfterLogon(false);
}
}
function MyComponent(props) {
const [isSignedIn, setisSignedIn] = useState(false);
const [afterLogon, setAfterLogon] = useState(false);
useEffect(() => {
firebase.auth().onAuthStateChanged(async (auth) => {
const authen = !!auth;
setisSignedIn(authen);
if (!isSignedIn) {
attemptToRestoreAuthAsync(setAfterLogon);
}
});
logonAsync(setAfterLogon);
}, [isSignedIn, setAfterLogon, afterLogon]);
return (
<NavigationContainer>
<Stack.Navigator headerMode="none">
{isSignedIn ? (
<>
{afterLogon ? (
<Stack.Screen name="Rotas" component={Rotas} />
) : (
<Stack.Screen
name="AfterLogin"
component={AfterLogin}
initialParams={{
user: firebase.auth().currentUser,
firebase,
setAfterLogon,
}}
/>
)}
</>
) : (
<Stack.Screen name="SignIn" component={SignInScreen} initialParams={{ setisSignedIn }}/>
)}
</Stack.Navigator>
<StatusBar style="dark" />
</NavigationContainer>
);
}
export default MyComponent;