Want to check for token expiration and logout user? In my view middleware will be the best option. You can do something like this:
1 2 3 4 5 6 7 8 9 10 | 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.
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.