-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
92 lines (84 loc) · 2.39 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Adapted from https://reactnavigation.org/docs/auth-flow
import * as React from 'react';
import {NavigationContainer, ParamListBase} from '@react-navigation/native';
import {
createStackNavigator,
StackNavigationProp,
} from '@react-navigation/stack';
import {AuthContext} from './AuthContext';
import SignInScreen from './screens/SignInScreen';
import DrawerMenuContent from './menus/DrawerMenu';
import AuthLoadingScreen from './screens/AuthLoadingScreen';
import {AuthManager} from './auth/AuthManager';
const Stack = createStackNavigator();
type Props = {
navigation: StackNavigationProp<ParamListBase>;
};
export default function App({navigation}: Props) {
const [state, dispatch] = React.useReducer(
(prevState: any, action: any) => {
switch (action.type) {
case 'RESTORE_TOKEN':
return {
...prevState,
userToken: action.token,
isLoading: false,
};
case 'SIGN_IN':
return {
...prevState,
isSignOut: false,
userToken: action.token,
};
case 'SIGN_OUT':
return {
...prevState,
isSignOut: true,
userToken: null,
};
}
},
{
isLoading: true,
isSignOut: false,
userToken: null,
},
);
React.useEffect(() => {
const bootstrapAsync = async () => {
let userToken = null;
// TEMPORARY
dispatch({type: 'RESTORE_TOKEN', token: userToken});
};
bootstrapAsync();
}, []);
const authContext = React.useMemo(
() => ({
signIn: async () => {
await AuthManager.signInAsync();
const token = await AuthManager.getAccessTokenAsync();
dispatch({type: 'SIGN_IN', token: token});
},
signOut: async () => {
await AuthManager.signOutAsync();
dispatch({type: 'SIGN_OUT'});
},
}),
[],
);
return (
<AuthContext.Provider value={authContext}>
<NavigationContainer>
<Stack.Navigator>
{state.isLoading ? (
<Stack.Screen name="Loading" component={AuthLoadingScreen} />
) : state.userToken == null ? (
<Stack.Screen name="SignIn" component={SignInScreen} />
) : (
<Stack.Screen name="Main" component={DrawerMenuContent} />
)}
</Stack.Navigator>
</NavigationContainer>
</AuthContext.Provider>
);
}