How to store tokens in react native?

前端 未结 6 1244
萌比男神i
萌比男神i 2021-02-05 05:59

I used to develop in android previously and i used to used SharePreference for storing user tokens. Is there anything such available in react native for both ios and android?

6条回答
  •  花落未央
    2021-02-05 06:05

    If you're using create-react-native-app or exp, you can use Expo.SecureStore to store your token.

    import {SecureStore} from 'expo';
    
    await SecureStore.setItemAsync('secure_token','sahdkfjaskdflas$%^&');
    const token = await SecureStore.getItemAsync('secure_token');
    console.log(token); // output: sahdkfjaskdflas$%^&
    

    Details: https://docs.expo.io/versions/latest/sdk/securestore

    Update in Dec 2020:

    The SecureStore module is now become expo-secure-store

    import * as SecureStore from 'expo-secure-store';
    
    await SecureStore.setItemAsync('secure_token','sahdkfjaskdflas$%^&');
    const token = await SecureStore.getItemAsync('secure_token');
    console.log(token); // output: sahdkfjaskdflas$%^&
    

提交回复
热议问题