Prevent the logoutAction()
from causing the middleware to dispatch logoutAction()
and so on...
if(action.type === 'your logoutAction type') return next(action);
Example:
const handleAction = (store) => (next) => (action) => {
if(action.type === 'your logoutAction type') return next(action);
const token = loadState(TOKEN);
const { userAccount } = store.getState();
if (token && userAccount.email) {
const decodedJwt = jwt_decode(token);
if (decodedJwt.exp < dayjs().unix()) {
store.dispatch(logoutAction());
}
}
return next(action);
};
You can also combine it with your existing condition:
const handleAction = (store) => (next) => (action) => {
const token = loadState(TOKEN);
const { userAccount } = store.getState();
if (action.type !== 'your logoutAction type' &&
token &&
userAccount.email) {
const decodedJwt = jwt_decode(token);
if (decodedJwt.exp < dayjs().unix()) {
store.dispatch(logoutAction());
}
}
return next(action);
};
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…