Skip to content

Instantly share code, notes, and snippets.

@Fabiantjoeaon
Created May 5, 2017 08:46
Show Gist options
  • Save Fabiantjoeaon/aa9d827a2cce37aef72fe3a4e8834764 to your computer and use it in GitHub Desktop.
Save Fabiantjoeaon/aa9d827a2cce37aef72fe3a4e8834764 to your computer and use it in GitHub Desktop.
Redux middleware for saving to local storage on action intercept
const localStorageMiddleware = store => next => action => {
if (action.type === REGISTER || action.type === LOGIN) {
if (!action.error) {
window.localStorage.setItem('jwt', action.payload.user.token);
agent.setToken(action.payload.user.token);
}
} else if (action.type === LOGOUT) {
window.localStorage.setItem('jwt', '');
agent.setToken(null);
}
next(action);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment