/
LocalAuthenticationScreen.tsx
95 lines (85 loc) · 2.79 KB
/
LocalAuthenticationScreen.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
import React from 'react';
import { Text, View } from 'react-native';
import * as LocalAuthentication from 'expo-local-authentication';
import Button from '../components/Button';
interface State {
waiting: boolean;
hasHardware?: boolean;
isEnrolled?: boolean;
}
export default class LocalAuthenticationScreen extends React.Component<{}, State> {
static navigationOptions = {
title: 'LocalAuthentication',
};
readonly state: State = {
waiting: false,
};
componentDidMount() {
this.checkDevicePossibilities();
}
async checkDevicePossibilities() {
const hasHardware = await LocalAuthentication.hasHardwareAsync();
const isEnrolled = await LocalAuthentication.isEnrolledAsync();
this.setState({ hasHardware, isEnrolled });
}
authenticate = async () => {
this.setState({ waiting: true });
try {
const result = await LocalAuthentication.authenticateAsync({
promptMessage: 'Authenticate',
cancelLabel: 'Cancel label',
disableDeviceFallback: true,
});
if (result.success) {
alert('Authenticated!');
} else {
alert('Failed to authenticate, reason: ' + result.error);
}
} finally {
this.setState({ waiting: false });
}
};
checkAuthenticationsTypes = async () => {
const result = await LocalAuthentication.supportedAuthenticationTypesAsync();
const stringResult = result
.map(type => {
switch (type) {
case LocalAuthentication.AuthenticationType.FINGERPRINT:
return 'FINGERPRINT';
case LocalAuthentication.AuthenticationType.FACIAL_RECOGNITION:
return 'FACIAL_RECOGNITION';
default:
throw new Error(`Unrecognised authentication type returned: '${type}'`);
}
})
.join(', ');
alert(stringResult ? `Available types: ${stringResult}` : 'No available authentication types!');
};
render() {
const { hasHardware, isEnrolled } = this.state;
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<View style={{ paddingBottom: 30 }}>
<Text>
LocalAuthentication.hasHardwareAsync():
<Text style={{ fontWeight: 'bold' }}>{` ${hasHardware}`}</Text>
</Text>
<Text>
LocalAuthentication.isEnrolledAsync():
<Text style={{ fontWeight: 'bold' }}>{` ${isEnrolled}`}</Text>
</Text>
</View>
<Button
style={{ margin: 5 }}
onPress={this.authenticate}
title={this.state.waiting ? 'Waiting for authentication... ' : 'Authenticate'}
/>
<Button
style={{ margin: 5 }}
onPress={this.checkAuthenticationsTypes}
title="Check available authentications types"
/>
</View>
);
}
}