How do I check for token expiration and logout user?
const parseJwt = (token) => {
const decode = JSON.parse(atob(token.split('.')[1]));
console.log(decode);
if (decode.exp * 1000 < new Date().getTime()) {
logoutAction();
console.log('Time Expired');
}
};
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