-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
SecureStoreScreen.tsx
117 lines (110 loc) · 3.13 KB
/
SecureStoreScreen.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
import React from 'react';
import { Alert, Platform, ScrollView, TextInput, View } from 'react-native';
import * as SecureStore from 'expo-secure-store';
import ListButton from '../components/ListButton';
interface State {
key?: string;
value?: string;
}
export default class SecureStoreScreen extends React.Component<{}, State> {
static navigationOptions = {
title: 'SecureStore',
};
readonly state: State = {};
_setValue = async (value: string, key: string) => {
try {
console.log('securestore: ' + SecureStore);
await SecureStore.setItemAsync(key, value, {});
Alert.alert(
'Success!',
'Value: ' + value + ', stored successfully for key: ' + key,
[{ text: 'OK', onPress: () => {} }]
);
} catch (e) {
Alert.alert('Error!', e.message, [{ text: 'OK', onPress: () => {} }]);
}
}
_getValue = async (key: string) => {
try {
const fetchedValue = await SecureStore.getItemAsync(key, {});
Alert.alert('Success!', 'Fetched value: ' + fetchedValue, [
{ text: 'OK', onPress: () => {} },
]);
} catch (e) {
Alert.alert('Error!', e.message, [{ text: 'OK', onPress: () => {} }]);
}
}
_deleteValue = async (key: string) => {
try {
await SecureStore.deleteItemAsync(key, {});
Alert.alert('Success!', 'Value deleted', [
{ text: 'OK', onPress: () => {} },
]);
} catch (e) {
Alert.alert('Error!', e.message, [{ text: 'OK', onPress: () => {} }]);
}
}
render() {
return (
<ScrollView
style={{
flex: 1,
padding: 10,
}}
>
<TextInput
style={{
marginBottom: 10,
padding: 10,
height: 40,
...Platform.select({
ios: {
borderColor: '#ccc',
borderWidth: 1,
borderRadius: 3,
},
}),
}}
placeholder="Enter a value to store (ex. pw123!)"
value={this.state.value}
onChangeText={value => this.setState({ value })}
/>
<TextInput
style={{
marginBottom: 10,
padding: 10,
height: 40,
...Platform.select({
ios: {
borderColor: '#ccc',
borderWidth: 1,
borderRadius: 3,
},
}),
}}
placeholder="Enter a key for the value (ex. password)"
value={this.state.key}
onChangeText={key => this.setState({ key })}
/>
{this.state.value && this.state.key && (
<ListButton
onPress={() => this._setValue(this.state.value!, this.state.key!)}
title="Store value with key"
/>
)}
{this.state.key && (
<ListButton
onPress={() => this._getValue(this.state.key!)}
title="Get value with key"
/>
)}
{this.state.key && (
<ListButton
onPress={() => this._deleteValue(this.state.key!)}
title="Delete value with key"
/>
)}
</ScrollView>
);
}
}