JetRockets profile image JetRockets

How to handle 401 unauthorized error in a Redux React application

github logo ・1 min read

In response to a client request, the server may return a 401 Unauthorized error. You must correctly catch it, for example, clear the state and redirect to the authorization page. To solve this problem, we wrote a custom Middleware which, in our opinion, is the best solution.

import * as actions from 'actions';

const authInterceptor = ({ dispatch }) => (next) => (action) => {
  if (action.status === 401) {
    dispatch(actions.removeJwt());
  } else {
    next(action);
  }
};
twitter logo DISCUSS
JetRockets is a technology consulting firm that architects, designs, develops and supports enterprise-level web, mobile and software platforms helping clients achieve their goals and grow their businesses.
Classic DEV Post from Dec 15 '18

The new MacBook Pros are overpriced

I've been a Mac user for over a decade but I'm lacking enthusiasm towards the r...