【问题标题】:SecureStore using expo returns Promise instead of JWT token in reducer file使用 expo 的 SecureStore 在 reducer 文件中返回 Promise 而不是 JWT 令牌
【发布时间】:2021-09-06 13:23:34
【问题描述】:

所以,我正在尝试获取我的减速器的 JWT 令牌 INITIAL_STATE

我正在使用 'expo-secure-store' 包作为使用 expo 的商店令牌

但是当我尝试获取令牌时,它返回的是一个承诺而不是令牌值,如下所示:

AuthUser: Promise {
  "_U": 0,
  "_V": 1,
  "_W": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo3LCJ1c2VybmFtZSI6InNocmVleWFjaEBzYXR2aXguY29tIiwiZW1haWwiOiJzaHJlZXlhY2hAc2F0dml4LmNvbSI",
  "_X": null,
}

这是我获取令牌的代码:

import * as SecureStorage from 'expo-secure-store';

const _retrieveData = async () => {
  try {
    const data = await SecureStorage.getItemAsync('token');
    return data;
  } catch (error) {
    console.log(error);
  }
};

const INIT_STATE = {
  loader: false,
  alertMessage: '',
  successMessage: '',
  showMessage: false,
  initURL: '',
  authUser: _retrieveData().then((token) => token),
  tempAuth: '',
  currentScreen: 0,
  showSuccessMessage: false,
  currentUser: null,
  sendOTP: false,
};

我尝试了hereherehere 的其他StackOverflow 解决方案,但无法将令牌作为字符串值。

【问题讨论】:

    标签: react-native expo


    【解决方案1】:

    在导出对象变化中,

    authUser: _retrieveData().then(token => token),
    

    authUser: _retrieveData,
    

    现在无论你想在哪里调用这个函数,都像这样使用它

    const response = await authUser();
    console.log(response) 
    // This will be the required token
    

    供参考,

    看看从here导出的存储对象

    你看,我在这里传递了对函数的引用。

    如上所述使用here

    【讨论】:

    • @AndroidPlayer_Shree 乐于助人!如果您的问题得到解决,请考虑接受答案,因为它可能对未来的用户有所帮助。谢谢
    猜你喜欢
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 2018-04-05
    • 2022-01-27
    • 2021-07-24
    相关资源
    最近更新 更多