简体   繁体   中英

Store access_token in the AsyncStorage

After user enters ID and password , I am passing access_token from Rails.

I am using itzikbenh/Rails-React-Auth and itzikbenh/React-Native-on-Rails as reference.

But I am unable to save the access_token . Here is the code:

let res = await response.text();
if (response.status >= 200 && response.status < 300) {
    //Handle success
    let accessToken = res;
    console.log(accessToken);
    //On success we will store the access_token in the AsyncStorage
    this.storeToken(accessToken);
    //this.redirect('home');
    alert(ACCESS_TOKEN )
} else {
    //Handle error
    let error = res;
    throw error;
}

You can use AsyncStorage.setItem to store single items:

import { ... AsyncStorage } from 'react-native'

try {
  await AsyncStorage.setItem('access_token', access_token);
} catch (error) { // Error saving data }

Then to retrieve it use AsyncStorage.getItem :

try {
  const value = await AsyncStorage.getItem('access_token');
  if (value !== null) console.log(value)
} catch (error) { // Error retrieving data }

For storing and retrieving multiple items you can see AsyncStorage.multiSet and AsyncStorage.multiGet .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM