SecureStore delete my token each time I refresh the

Please provide the following:

  1. SDK Version: 36
  2. Platforms(Android/iOS/web/all): IOS

I have problem retrieving my authentication token from SecureStore while I develop my app in the expo client.

I’m trying to implement this authentication flow from react navigation using secureStore instead of Async storage: https://reactnavigation.org/docs/en/4.x/auth-flow.html

Currently, when I login my token is stored in SecureStore. I know this because I can then use it for authorising my requests.

My problem is when the expo app refreshes, the token seems to disappear so I have to sign in again.

Does the expo client app refreshing clear the token or is there a bug in my code:

 useEffect(() => {
    _bootstrapAsync = async () => {
      const token = await SecureStore.getItemAsync('token')
      navigation.navigate(token ? 'App' : 'Auth');
      
    };
    _bootstrapAsync();
  }, []) 

you can run this code in your app to verify that it does indeed work as expected and there is likely some issue in your own code:

import * as React from 'react';
import { Button, Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import * as SecureStore from 'expo-secure-store';

export default function App() {
  let [storedValue, setStoredValue] = React.useState(null);

  async function getValueAsync() {
    let result = await SecureStore.getItemAsync('example');
    setStoredValue(result);
  }

  async function setValueAsync() {
    SecureStore.setItemAsync('example', 'some-text!');
  }

  React.useEffect(() => {
    getValueAsync();
  }, []);

  return (
    <View style={styles.container}>
      <Text style={styles.paragraph}>{storedValue}</Text>
      <Button
        title="Store 'some-text!' in 'example' key"
        onPress={setValueAsync}
      />
      <Button
        title="Get value from 'example' key"
        onPress={getValueAsync}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  paragraph: {
    margin: 24,
    fontSize: 18,
    fontWeight: 'bold',
    textAlign: 'center',
  },
});

Hi @notbrent thanks for responding. My code works, I can get the token in the app. My question really is whether secureStore gets cleared when the expo client app refreshes?

securestore is not cleared when the app reloads or when you close/reopen the app - you can verify that with the example i provided. the point of the example was to demonstrate in a minimal context that the issue you describe is not reproducible

Hi @notbrent, thank you for helping me with this. I did in fact find that the issue was my fault. When the app reloads redux state is refreshed. I had inexplicably left a ‘SecureStore.deleteItem(‘token’)’ in my store.js file. That caused it to be deleted every time it was refreshed.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.