我曾经在android中开发过以前我习惯使用SharePreference来存储用户令牌.ios和android有什么可用的原生作用吗?
小智 17
如果您正在使用create-react-native-app或exp,则可以Expo.SecureStore用来存储令牌.
import {SecureStore} from 'expo';
await SecureStore.setItemAsync('secure_token','sahdkfjaskdflas$%^&');
const token = await SecureStore.getItemAsync('secure_token');
console.log(token); // output: sahdkfjaskdflas$%^&
Run Code Online (Sandbox Code Playgroud)
详情:https://docs.expo.io/versions/latest/sdk/securestore
fla*_*aky 11
作为其他答案的补充,您可能需要在存储令牌时考虑在用户设备上加密令牌。
因此,除了通过AsyncStorage存储它之外,您可能还想使用类似以下方法:react-native-keychain在设备上对其进行加密。
你可能想使用AsyncStorage
因为AsyncStorage现在已弃用。您可以使用https://github.com/react-native-community/async-storage
编辑:
对于指出 AsyncStorage 不安全的每个人,请参阅这篇文章。
以下是一些在 React Native 中存储持久数据的方法:
async-storage存储未加密的键值数据。不要使用异步存储来存储令牌、秘密和其他机密数据。一定要使用异步存储来持久化 Redux 状态、GraphQL 状态和存储全局应用程序范围的变量。
react-native-keychain以字符串格式将用户名/密码组合存储在安全存储中。存储/访问它时使用 JSON.stringify/JSON.parse。
react-native-sensitive-info - 适用于 iOS,但使用适用于 Android 的 Android 共享首选项(默认情况下不安全)。然而,有一个fork ) 使用 Android Keystore。
redux-persist-sensitive-storage - 包装 react-native-sensitive-info
有关React Native 存储和安全性的更多信息
使用博览会安全商店
// to install it 'expo install expo-secure-store'
import * as SecureStore from 'expo-secure-store';
const setToken = (token) => {
return SecureStore.setItemAsync('secure_token', token);
};
const getToken = () => {
return SecureStore.getItemAsync('secure_token');
};
setToken('#your_secret_token');
getToken().then(token => console.log(token)); // output '#your_secret_token'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15082 次 |
| 最近记录: |