-
Notifications
You must be signed in to change notification settings - Fork 47
/
AppNavigator.tsx
201 lines (163 loc) · 6.38 KB
/
AppNavigator.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import {NavigationContainer, NavigationContainerRef} from '@react-navigation/native'
import {createStackNavigator} from '@react-navigation/stack'
import React, {useEffect} from 'react'
import {defineMessages, useIntl} from 'react-intl'
import {Alert, AppState, AppStateStatus, Platform} from 'react-native'
import RNBootSplash from 'react-native-bootsplash'
import StorybookScreen from '../.storybook'
import {OsLoginScreen, PinLoginScreen, useBackgroundTimeout} from './auth'
import {useAuth} from './auth/AuthProvider'
import {EnableLoginWithPin} from './auth/EnableLoginWithPin'
import {FirstRunNavigator} from './FirstRun/FirstRunNavigator'
import {DeveloperScreen} from './legacy/DeveloperScreen'
import {AppRoutes} from './navigation'
import {WalletInitNavigator} from './WalletInit/WalletInitNavigator'
import {WalletNavigator} from './WalletNavigator'
import {AuthSetting, useAuthOsEnabled, useAuthSetting, useAuthWithOs} from './yoroi-wallets'
const Stack = createStackNavigator<AppRoutes>()
const navRef = React.createRef<NavigationContainerRef<ReactNavigation.RootParamList>>()
export const AppNavigator = () => {
const strings = useStrings()
useHideScreenInAppSwitcher()
useAutoLogout()
const {isLoggedIn, isLoggedOut, login} = useAuth()
const {authWithOs} = useAuthWithOs({
onSuccess: login,
onSettled: () => RNBootSplash.hide({fade: true}),
})
const authAction = useAuthAction()
const onReady = () => {
if (isLoggedIn) return
// try first OS auth before navigating to os login screen
if (authAction === 'auth-with-os') {
authWithOs()
} else {
RNBootSplash.hide({fade: true})
}
}
// RNBootSplash.hide({fade: true})
// return <StorybookScreen />
return (
<NavigationContainer onReady={onReady} ref={navRef}>
<Stack.Navigator
screenOptions={{
headerShown: false /* used only for transition */,
detachPreviousScreen: false /* https://github.com/react-navigation/react-navigation/issues/9883 */,
}}
>
{/* Not Authenticated */}
{isLoggedOut && (
<Stack.Group>
{authAction === 'first-run' && <Stack.Screen name="first-run" component={FirstRunNavigator} />}
{authAction === 'auth-with-pin' && (
<Stack.Screen
name="custom-pin-auth"
component={PinLoginScreen}
options={{title: strings.loginPinTitle}}
/>
)}
{authAction === 'auth-with-os' && (
<Stack.Screen name="bio-auth-initial" component={OsLoginScreen} options={{headerShown: false}} />
)}
{authAction === 'request-new-pin' && (
<Stack.Screen //
name="enable-login-with-pin"
component={CreatePinScreenWrapper}
options={{title: strings.customPinTitle}}
/>
)}
</Stack.Group>
)}
{/* Authenticated */}
{isLoggedIn && (
<Stack.Group>
<Stack.Screen name="app-root" component={WalletNavigator} />
<Stack.Screen name="new-wallet" component={WalletInitNavigator} />
</Stack.Group>
)}
{/* Development */}
{__DEV__ && (
<Stack.Group>
<Stack.Screen name="developer" component={DeveloperScreen} options={{headerShown: false}} />
<Stack.Screen name="storybook" component={StorybookScreen} />
</Stack.Group>
)}
</Stack.Navigator>
</NavigationContainer>
)
}
export default AppNavigator
const CreatePinScreenWrapper = () => {
const {login} = useAuth()
return <EnableLoginWithPin onDone={login} />
}
const useStrings = () => {
const intl = useIntl()
return {
customPinTitle: intl.formatMessage(messages.customPinTitle),
loginPinTitle: intl.formatMessage(messages.pinLoginTitle),
authWithOsChangeTitle: intl.formatMessage(messages.authWithOsChangeTitle),
authWithOsChangeMessage: intl.formatMessage(messages.authWithOsChangeMessage),
}
}
const messages = defineMessages({
pinLoginTitle: {
id: 'components.login.custompinlogin.title',
defaultMessage: '!!!Enter PIN',
},
customPinTitle: {
id: 'components.firstrun.custompinscreen.title',
defaultMessage: '!!!Set PIN',
},
authWithOsChangeTitle: {
id: 'global.actions.dialogs.walletKeysInvalidated.title',
defaultMessage: '!!!Auth with OS changes',
},
authWithOsChangeMessage: {
id: 'global.actions.dialogs.biometricsChange.message',
defaultMessage: '!!!Auth with OS changed detected ',
},
})
const useAutoLogout = () => {
const authSetting = useAuthSetting()
const strings = useStrings()
const {logout} = useAuth()
const authOsEnabled = useAuthOsEnabled()
const osAuthDisabled = !authOsEnabled && authSetting === 'os'
useBackgroundTimeout({
onTimeout: logout,
duration: 120 * 1000,
})
React.useEffect(() => {
if (osAuthDisabled) {
Alert.alert(strings.authWithOsChangeTitle, strings.authWithOsChangeMessage)
logout()
}
}, [osAuthDisabled, strings.authWithOsChangeTitle, strings.authWithOsChangeMessage, logout])
}
const useHideScreenInAppSwitcher = () => {
const appStateRef = React.useRef(AppState.currentState)
useEffect(() => {
const subscription = AppState.addEventListener('change', (nextAppState: AppStateStatus) => {
if (Platform.OS !== 'ios') return
const isFocused = (appState: AppStateStatus) => appState === 'active'
const isBlurred = (appState: AppStateStatus) => appState === 'inactive' || appState === 'background'
if (isBlurred(appStateRef.current) && isFocused(nextAppState)) RNBootSplash.hide({fade: true})
if (isFocused(appStateRef.current) && isBlurred(nextAppState)) RNBootSplash.show({fade: true})
appStateRef.current = nextAppState
})
return () => subscription?.remove()
}, [])
}
type AuthAction = 'auth-with-pin' | 'auth-with-os' | 'request-new-pin' | 'first-run'
const getAuthAction = (authOsEnabled: boolean, authSetting: AuthSetting): AuthAction => {
if (authSetting === 'pin') return 'auth-with-pin'
if (authSetting === 'os' && authOsEnabled) return 'auth-with-os'
if (authSetting === 'os' && !authOsEnabled) return 'request-new-pin'
return 'first-run' // setup not completed
}
const useAuthAction = () => {
const authSetting = useAuthSetting()
const authOsEnabled = useAuthOsEnabled()
return getAuthAction(authOsEnabled, authSetting)
}