import * as SecureStore from 'expo-secure-store';
import * as React from 'react';
import {
Alert,
Platform,
ScrollView,
Text,
TextInput,
View,
Switch,
StyleSheet,
} from 'react-native';
import ListButton from '../components/ListButton';
import Colors from '../constants/Colors';
import { useResolvedValue } from '../utilities/useResolvedValue';
export default function SecureStoreScreen() {
const [isAvailable, error] = useResolvedValue(SecureStore.isAvailableAsync);
const warning = React.useMemo(() => {
if (error) {
return `An unknown error occurred while checking the API availability: ${error.message}`;
} else if (isAvailable === null) {
return 'Checking availability...';
} else if (isAvailable === false) {
return 'SecureStore API is not available on this platform.';
}
return null;
}, [error, isAvailable]);
if (warning) {
return (
{warning}
);
}
return ;
}
function SecureStoreView() {
const [key, setKey] = React.useState();
const [value, setValue] = React.useState();
const [service, setService] = React.useState();
const [requireAuth, setRequireAuth] = React.useState();
const toggleAuth = async () => {
setRequireAuth(!requireAuth);
};
async function storeValueAsync(value: string, key: string) {
try {
await SecureStore.setItemAsync(key, value, {
keychainService: service,
requireAuthentication: requireAuth,
authenticationPrompt: 'Authenticate',
});
Alert.alert('Success!', 'Value: ' + value + ', stored successfully for key: ' + key, [
{ text: 'OK', onPress: () => {} },
]);
} catch (e) {
Alert.alert('Error!', e.message, [{ text: 'OK', onPress: () => {} }]);
}
}
function storeValue(value: string, key: string) {
try {
SecureStore.setItem(key, value, {
keychainService: service,
requireAuthentication: requireAuth,
authenticationPrompt: 'Authenticate',
});
Alert.alert('Success!', 'Value: ' + value + ', stored successfully for key: ' + key, [
{ text: 'OK', onPress: () => {} },
]);
} catch (e) {
Alert.alert('Error!', e.message, [{ text: 'OK', onPress: () => {} }]);
}
}
async function getValueAsync(key: string) {
try {
const fetchedValue = await SecureStore.getItemAsync(key, {
keychainService: service,
requireAuthentication: requireAuth,
authenticationPrompt: 'Authenticate',
});
Alert.alert('Success!', 'Fetched value: ' + fetchedValue, [
{ text: 'OK', onPress: () => {} },
]);
} catch (e) {
Alert.alert('Error!', e.message, [{ text: 'OK', onPress: () => {} }]);
}
}
function getValue(key: string) {
try {
const fetchedValue = SecureStore.getItem(key, {
keychainService: service,
requireAuthentication: requireAuth,
authenticationPrompt: 'Authenticate',
});
Alert.alert('Success!', 'Fetched value: ' + fetchedValue, [
{ text: 'OK', onPress: () => {} },
]);
} catch (e) {
Alert.alert('Error!', e.message, [{ text: 'OK', onPress: () => {} }]);
}
}
async function deleteValue(key: string) {
try {
await SecureStore.deleteItemAsync(key, { keychainService: service });
Alert.alert('Success!', 'Value deleted', [{ text: 'OK', onPress: () => {} }]);
} catch (e) {
Alert.alert('Error!', e.message, [{ text: 'OK', onPress: () => {} }]);
}
}
return (
Requires authentication:
{value && key && (
storeValueAsync(value, key)} title="Store value with key" />
)}
{key && getValueAsync(key)} title="Get value with key" />}
{value && key && (
storeValue(value, key)}
title="Store value with key synchronously"
/>
)}
{key && getValue(key)} title="Get value with key synchronously" />}
{key && deleteValue(key)} title="Delete value with key" />}
);
}
SecureStoreScreen.navigationOptions = {
title: 'SecureStore',
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 10,
},
textInput: {
marginBottom: 10,
padding: 10,
height: 40,
...Platform.select({
ios: {
borderColor: '#ccc',
borderWidth: 1,
borderRadius: 3,
},
}),
},
authToggleContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
},
});