在AsyncStorage中存储access_token

时间:2017-07-09 20:45:44

标签: ruby-on-rails ruby-on-rails-4 react-native

用户输入IDpassword后,我将从Rails传递access_token

我正在使用 itzikbenh/Rails-React-Authitzikbenh/React-Native-on-Rails作为参考。

但我无法保存access_token。这是代码:

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;
}

1 个答案:

答案 0 :(得分:1)

您可以使用AsyncStorage.setItem存储单个项目:

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

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

然后使用AsyncStorage.getItem

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

要存储和检索多个项目,您可以看到AsyncStorage.multiSetAsyncStorage.multiGet