How do I check for token expiration and logout user?

后端 未结 3 1099
傲寒
傲寒 2020-12-24 12:46

The user can logout himself when he/she clicks on the logout button but if the token is expired he/she cant logout because in my application, the token is used in both serve

3条回答
  •  無奈伤痛
    2020-12-24 13:03

    In my view middleware will be the best option.

    You can do something like this

    const checkTokenExpirationMiddleware = store => next => action => {
      const token =
        JSON.parse(localStorage.getItem("user")) &&
        JSON.parse(localStorage.getItem("user"))["token"];
      if (jwtDecode(token).exp < Date.now() / 1000) {
        next(action);
        localStorage.clear();
      }
      next(action);
    };
    

    You have to then wrap it in applyMiddleware

提交回复
热议问题