Created
May 5, 2017 08:46
-
-
Save Fabiantjoeaon/aa9d827a2cce37aef72fe3a4e8834764 to your computer and use it in GitHub Desktop.
Redux middleware for saving to local storage on action intercept
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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