My current approach is to store the jwt in the localStorage, when the application starts, I am loading the token, trying to load user data with the token, if it fails, just redirect to /login

Then I am using the jwt only in the api module, not in the store at all.

My Api module knows based on requests, when to use the token and when not

If the api module recognizes a failed authentication, it also removes the token from the localStorage.. so next time it cannot be loaded.

My api module, which is also separated from redux, knows when to use jwt, and when not.

To make this work more abstract I've created a middleware, which reacts on every action if the payload is an error and the value Not Authenticated

this is the error I am trowing in the api module, if the server response fails because of auth. The actionCreator just dispatches the error in catch and the middleware reacts on it.

So actually its up to you how to do it, all the code I am talking about is ~100 LOC or so.. just some methods which are handling this things.

Related Query

More Query from same tag