One of the most frequently asked questions about React, Redux and REST is where to put authentication (OAuth tokens for example), how to wire it to Redux and how to add tokens to API calls.
Since user auth is a critical part of appit makes sense to put it into Redux . I suggest to add an API call method to Redux by utilizing the middleware’s functionality and dispatch from API actions by using . Let’s begin with configuration:
Note that we first initialize emptythat we will access later in function because we need function as parameter for which is used for creation. This can be done in a more elegant way, of course, but for simplicity we do it this brutal way.
Then you cana returned from API method that becomes available in actions ( makes it possible):
In reducer you can now also invalidate token on HTTPerrors:
This all makes your appto be always consistent, you can capture network auth-related errors in the same place where you take care of other token-related processes.