1

I have an issue with react/redux and authentication - the authentication part works well, I can log in and log out. The problem occurs when I log out with one user and log in with another - it occasionally displays the data of the previously logged in user.

I am using react and redux. I can see the data in redux is cleared on log out, all user or user profile information is removed. The token is also correctly removed from local storage. When I log in with a different user, I can see in the redux devtools that the correct data is fetched, however, the data displayed on screen is different and is the data from the user previously logged in.

If I refresh the page the correct information is then displayed.

My question is, where else is that data stored? is there a cache somewhere that I need to clear?

My reducer looks like this:

case LOGOUT:
    localStorage.removeItem('token');
    return{
      ...state,
      user: null,
      token: null,
      isAuthenticated: false,
      loading: false
    };

I can see all gets cleared out as expected.. I'm not sure where the data comes from?

viviejed
  • 33
  • 6
  • Can you post your code? – Rohit Kashyap Aug 23 '19 at 21:02
  • your current state is somehow getting preserved. make sure that you clear all user related states (like in a userReducer or sth) after logging out. – punjira Aug 24 '19 at 07:44
  • @saeedghotb I do clear the data in my reducer (see above updated question shows the bit of my reducer that does just that). I can see in the devtools that my redux store appears empty so not sure where the data is held? I'm not using redux-persist either. Any suggestions welcome! :) – viviejed Aug 24 '19 at 13:10

2 Answers2

1

Found it in the end... I was not deleting the authorization header, this was causing the API to be called with the previous user data before the headers were reset and the API called again with the right user data.... Sometimes the data was returned quick enough to show the correct user logged in...sometimes not. This line in logout reducer:

delete axios.defaults.headers.common['Authorization'];

solved it!

viviejed
  • 33
  • 6
0

Maybe you can solve your problem resetting redux state https://stackoverflow.com/a/35641992/11969290

  • Thanks I tried this but the problem is still the same .. I was already resetting the all user states in the reducer but I like that this resets it fully at the top level. Anyway, I can see that my store is empty in the dev tools so I don't know where it's getting that data from? – viviejed Aug 24 '19 at 09:36
  • Try clearing the browser cache, or using another browser – Gabriel Pérez Aug 25 '19 at 00:18