How to store tokens in react native?

前端 未结 6 1247
萌比男神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:27

    use expo-secure-store

    // 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'
    

提交回复
热议问题