How to use Redux to refresh JWT token?
<p>Our React Native Redux app uses JWT tokens for authentication. There are many actions that require such tokens and a lot of them are dispatched simultaneously e.g. when app loads.</p>

<p>E.g.</p>

<pre><code>componentDidMount() {
    dispath(loadProfile());
    dispatch(loadAssets());
    ...
}
</code></pre>

<p>Both <code>loadProfile</code> and <code>loadAssets</code> require JWT. We save the token in the state and <code>AsyncStorage</code>. My question is how to handle token expiration.</p>

<p>Originally I was going to use middleware for handling token expiration</p>

<pre><code>// jwt-middleware.js

export function refreshJWTToken({ dispatch, getState }) {

  return (next) =&gt; (action) =&gt; {
    if (isExpired(getState().auth.token)) {
      return dispatch(refreshToken())
          .then(() =&gt; next(action))
          .catch(e =&gt; console.log('error refreshing token', e));
    }
    return next(action);
};
</code></pre>

<p>}</p>

<p>The problem that I ran into was that refreshing of the token will happen for both <code>loadProfile</code> and <code>loadAssets</code> actions because at the time when they are dispatch the token will be expired. Ideally I would like to "pause" actions that require authentication until the token is refreshed. Is there a way to do that with middleware?</p>