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 Jan 14

Who's looking for open source contributors? (Jan 14th edition)

Find something to work on or promote your project here. Please shamelessly pro...

Sore eyes?

dev.to now has dark mode.

Go to the "misc" section of your settings and select night theme ❤️