Search code examples
react-nativeaxiosinterceptorasyncstorage

react-native-community/asyncStorage removeItem causes program to behave weirdly


I have this little code snippet executed during the user logout.

 async function logoutAction(props) {
  removeUser();
  props.logoutUser();
}

The function inside removeUser() is as :

 export const removeUser = async () => {
  try {
    await AsyncStorage.removeItem(Constant.storage.user_data);
    await AsyncStorage.removeItem(Constant.storage.token);
    await AsyncStorage.removeItem(Constant.storage.notification_token);
    return true;
  } catch (exception) {
    return false;
  }
}

This clears user related data from local storage. Similarly, props.logoutUser() is a reference call to reducer which sets loggedIn status to false.

I'm having this issue that if the removeUser() function is called once, the axios http requests do not enter the interceptors anymore and every request catches an error 'undefined'. If this method is removed though, everything works fine. I can get it to working state then by removing the interceptors once, performing a request and then adding the interceptors again, which I found after hours of here and there.

My interceptors are:

    export const requestInterceptor = axios.interceptors.request.use(
  async config => {
    const token = await getToken();
    if (token != '') {
      config.headers.Authorization = token;
    }
    console.log('axios request', config);
    return config;
  },
  error => {
    // console.warn('on request error')
    return Promise.reject(error);
  },
);

export const responseInterceptor = axios.interceptors.response.use(
  function(response) {
    console.log('axios response', response);
    // console.warn('on response success', response.status)
    return response;
  },
  async function(error) {
    if (error.response.status === 401) {
      //logout user
      return;
    }
    return Promise.reject(error);
  },
);

I am using the @react-native-community/AsyncStorage package for maintaining local storage. I suspect that the issue might be in the removeItem method but I'm unsure as the official docs don't contain the removeItem method, or in the interceptor which doesn't seem faulty to me anyways.

What am I doing wrong here?? Please show me some light..


Solution

  • The issue was quite silly and did not even concern AsyncStorage or removeItem and as Matt Aft pointed out in the comment, it was due to the call for token in the interceptor after it had been removed while logging out. So, replacing

    const token = await getToken();
        if (token != '') {
          config.headers.Authorization = token;
        }
    

    by

      await getToken()
              .then(token => {
                config.headers.Authorization = token;
              })
              .catch(_ => {
                console.log('no token');
              });
    

    in the interceptor and returning promise from the getToken method did the thing. Thanks to Matt and 高鵬翔.