expo secure store은 키-값 쌍을 암호화하여 기기에 로컬로 안전하게 저장하는 방법을 제공합니다. 각 엑스포 프로젝트에는 별도의 저장 시스템이 있으며 다른 엑스포 프로젝트의 저장소에는 액세스할 수 없습니다. (공식문서)
npx expo install expo-secure-store
app.json
파일에 다음과 같이 설정을 추가합니다.{
"expo": {
"plugins": [
[
"expo-secure-store",
{
"configureAndroidBackup": true,
"faceIDPermission": "Allow $(PRODUCT_NAME) to access your Face ID biometric data."
}
]
]
}
}
npx pod-install
ios.config.usesNonExemptEncryption
속성을 false로 설정하여 암호화를 사용하지 않는다고 코드 상으로 명시할 수 있습니다.{
"expo": {
"ios": {
"config": {
"usesNonExemptEncryption": false
}
}
}
}
import * as SecureStore from 'expo-secure-store';
import { useState } from 'react';
import { Text, View, StyleSheet, TextInput, Button, Alert } from 'react-native';
async function save(key: string, value: string) {
await SecureStore.setItemAsync(key, value);
}
async function getValueFor(key: string) {
let result = await SecureStore.getItemAsync(key);
if (result) {
Alert.alert("🔐 Here's your value 🔐 \n" + result);
} else {
Alert.alert('No values stored under that key.');
}
}
const App = () => {
const [key, onChangeKey] = useState('Your key here');
const [value, onChangeValue] = useState('Your value here');
return (
<View style={styles.container}>
<Text style={styles.paragraph}>Save an item, and grab it later!</Text>
{}
<TextInput
style={styles.textInput}
clearTextOnFocus
onChangeText={(text) => onChangeKey(text)}
value={key}
/>
<TextInput
style={styles.textInput}
clearTextOnFocus
onChangeText={(text) => onChangeValue(text)}
value={value}
/>
{}
<Button
title="Save this key/value pair"
onPress={() => {
save(key, value);
onChangeKey('Your key here');
onChangeValue('Your value here');
}}
/>
<Text style={styles.paragraph}>🔐 Enter your key 🔐</Text>
<TextInput
style={styles.textInput}
onSubmitEditing={(event) => {
getValueFor(event.nativeEvent.text);
}}
placeholder="Enter the key for the value you want to get"
/>
</View>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: 10,
backgroundColor: '#ecf0f1',
padding: 8,
},
paragraph: {
marginTop: 34,
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
textInput: {
height: 35,
borderColor: 'gray',
borderWidth: 0.5,
padding: 4,
},
});
SecureStore.setItemAsync(key, value)
로 값을 저장하고, SecureStore.getItemAsync(key)
로 값을 가져오는 방법을 보여줍니다. 일반적인 AsyncStorage와 사용법이 비슷하지만, 더 안전하게 데이터를 저장할 수 있다는 장점이 있습니다.
SecureStore.deleteItemAsync(key, options) 메서드를 사용하여 특정 키의 값을 삭제할 수 있습니다. 또한 SecureStore.deleteAllItemsAsync() 메서드를 사용하여 모든 값을 삭제할 수 있습니다.requireAuthentication
속성으로 생체 인식 여부, authenticationPrompt
속성으로 인증 메시지를 추가할 수 있습니다.
SecureStore.canUseBiometricAuthentication() 메서드를 사용하여 기기에서 생체 인식을 사용할 수 있는지 확인할 수 있습니다.// ...
async function save(key: string, value: string) {
if (SecureStore.canUseBiometricAuthenticationAsync()) {
await SecureStore.setItemAsync(key, value, {
requireAuthentication: true,
authenticationPrompt: 'Authenticate to save the value',
});
} else {
await SecureStore.setItemAsync(key, value);
}
}
// ...