简体   繁体   English

世博安全商店。 身份验证后无法保存用户 ID 和令牌

[英]Expo Securestore. Can't save userid and token after auth

I am trying to save userId and token in securestore.我正在尝试将用户 ID 和令牌保存在安全存储中。 On the next screen I can get token from securestore, but I can't see userid在下一个屏幕上,我可以从 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); }
    }
  }

And here's the json returned by api call这是 api 调用返回的 json

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

You are not awaiting the async functions setItemAsync .您不是在等待异步函数setItemAsync You should try the following:您应该尝试以下操作:

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

Additionally, you can try to log the value of the json variable to make sure that it does contain the userId and token properties.此外,您可以尝试记录json变量的值,以确保它确实包含userIdtoken属性。

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

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