简体   繁体   English

将access_token存储在AsyncStorage中

[英]Store access_token in the AsyncStorage

After user enters ID and password , I am passing access_token from Rails. 用户输入IDpassword ,我将从Rails传递access_token

I am using itzikbenh/Rails-React-Auth and itzikbenh/React-Native-on-Rails as reference. 我正在使用itzikbenh / Rails-React-Authitzikbenh / React-Native-Rails

But I am unable to save the access_token . 但是我无法保存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: 您可以使用AsyncStorage.setItem存储单个项目:

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 : 然后使用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 . 有关存储和检索多个项目的信息,请参见AsyncStorage.multiSetAsyncStorage.multiGet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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