【问题标题】:Expo Securestore. Can't save userid and token after auth世博安全存储。身份验证后无法保存用户标识和令牌
【发布时间】:2021-12-08 04:30:59
【问题描述】:

我正在尝试将用户 ID 和令牌保存在安全存储中。在下一个屏幕上,我可以从 Securestore 获取令牌,但我看不到用户 ID

  const doUserLogIn = async() => {
    try {
      const response = await fetch('http://someurl-dev.de/api/login/?email='+email+'&password='+password, {
        method: 'POST',
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json'
        }
      });
      const json = await response.json();
      SecureStore.setItemAsync('userid', json.userId);
      SecureStore.setItemAsync('token', json.token);
    } catch (e) {
      alert('Error1:'+e);
    } finally {
      try{
        navigation.navigate('Schichten');
      } catch(e) {alert('Error2:'+e); }
    }
  }

这是api调用返回的json

{"userId":9,"token":"957a230f3eb6e63c47f1d7a0805fe31df6d2ced7","vorname":"Ankit","nachname":"Vij"}

【问题讨论】:

    标签: javascript android reactjs react-native frontend


    【解决方案1】:

    您不是在等待异步函数setItemAsync。您应该尝试以下方法:

    await SecureStore.setItemAsync('userid', json.userId);
    await SecureStore.setItemAsync('token', json.token);
    

    此外,您可以尝试记录json 变量的值,以确保它确实包含userIdtoken 属性。

    【讨论】:

      猜你喜欢
      • 2010-09-27
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 2021-06-24
      • 2013-02-12
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      相关资源
      最近更新 更多