Search code examples
reactjsreduxredux-thunk

Jwt expiration control and redirect login page


The user has a valid jwt for a certain period of time. this jwt ends if no activity is performed within 30 minutes. How can I redirect user to login page in middleware after jwt time is over.

return (next) => (action) => {
    if (typeof action === 'function') {
      if (getToken() && !getState().auth.pendingRefreshingToken) {
        const tokenExp = new Date(getTokenExp() * 1000);
        if (new Date() < tokenExp) { // JWT Expiration Control
          const refreshThreshold = new Date((new Date().getTime() + 300000));
          if (refreshThreshold > new Date(getTokenExp() * 1000)) { 
            refreshToken(dispatch).then(() => next(action)); // JWT Refresh
          }
        } else {
          logoutMiddleware(dispatch); // JWT Expiration(I want to redirect user to login page)
        }
      }
    }
    return next(action);
  };

Solution

  • I'm not sure if I understand your question correctly but assuming you have a specific route for login and you just want to redirect the user to that route you can do this:

    window.location.href = '/login'