简体   繁体   中英

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

I am trying to save userId and token in securestore. On the next screen I can get token from securestore, but I can't see userid

  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

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

You are not awaiting the async functions 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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