-
Notifications
You must be signed in to change notification settings - Fork 19
/
App.js
145 lines (122 loc) · 3.38 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
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, {createContext, useContext, useState, useEffect} from 'react';
import Entry from './app/screens/Entry';
import Locked from './app/screens/Locked';
import Loading from './app/screens/Loading';
import QRReader from './app/screens/QRReader';
import QRShow from './app/screens/QRShow';
import QRResult from './app/screens/QRResult';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { ThemeProvider } from './app/themes/ThemeProvider';
import { AuthContext } from './app/AuthContext';
import * as LocalAuthentication from 'expo-local-authentication'
import { AppState } from "react-native";
const Stack = createStackNavigator();
const AppStack = () => {
return (
<Stack.Navigator>
<Stack.Screen
name="Home"
component={Entry}
options={{ headerShown: false }}
/>
<Stack.Screen
name="QRReader"
component={QRReader}
options={{ title: 'Point to the QR Code' }}
/>
<Stack.Screen
name="QRShow"
component={QRShow}
options={{ title: 'Share QR' }}
/>
<Stack.Screen
name="QRResult"
component={QRResult}
options={{ title: 'Verification Complete' }}
/>
</Stack.Navigator>
);
};
const AuthStack = () => {
return (
<Stack.Navigator>
<Stack.Screen
name="Locked"
component={Locked}
options={{ headerShown: false }}
/>
</Stack.Navigator>
);
};
const Router = () => {
const {authData, loading, signIn, signOut} = useContext(AuthContext);
useEffect(() => {
AppState.addEventListener("change", _handleAppStateChange);
return () => {
AppState.removeEventListener("change", _handleAppStateChange);
};
}, []);
const _handleAppStateChange = (nextAppState) => {
if (nextAppState !== "active") {
signOut();
}
};
if (loading) {
return ( <Loading /> );
}
return (
<NavigationContainer>
{authData ? <AppStack /> : <AuthStack />}
</NavigationContainer>
);
};
const AuthProvider = ({children}) => {
const [authData, setAuthData] = useState(undefined);
const [loading, setLoading] = useState(undefined);
const loadBiometricSupport = async () => {
if (loading === undefined) {
setLoading(true);
if (!await LocalAuthentication.hasHardwareAsync() || !await LocalAuthentication.isEnrolledAsync()) {
console.log("Default Signed in", authData, loading);
signIn();
}
console.log("Loading End", authData, loading);
setLoading(false);
}
};
useEffect(() => {
loadBiometricSupport();
});
const signIn = async () => {
const _authData = 'authenticated'
setAuthData(_authData);
};
const signOut = async () => {
console.log("Sign out", authData, loading);
if (await LocalAuthentication.hasHardwareAsync() && await LocalAuthentication.isEnrolledAsync())
setAuthData(undefined);
};
return (
<AuthContext.Provider value={{authData, loading, signIn, signOut}}>
{children}
</AuthContext.Provider>
);
};
const App = () => {
return (
<ThemeProvider>
<AuthProvider>
<Router />
</AuthProvider>
</ThemeProvider>
);
};
export default App;