reddice
reddice copied to clipboard
Handle error jwtDecode
https://github.com/Remchi/reddice/blob/master/client/index.js#L24
jwtDecode can throw an error if a not valid token is passed, how do you suggest handling this?
for now I've created utils/decodeToken.js
with:
import jwtDecode from 'jwt-decode';
export default function decodeToken(token) {
let decoded = {};
try {
decoded = jwtDecode(token);
} catch (err) {
}
return decoded;
};
and in index.js
just call it instead of jwtDecode
import decodeToken from './utils/decodeToken';
...
store.dispatch(setCurrentUser(decodeToken(localStorage.jwtToken)));
However I think if it fails, something has to happen, like a redirect or clean up of the invalid token ?