reddice icon indicating copy to clipboard operation
reddice copied to clipboard

Handle error jwtDecode

Open dciccale opened this issue 8 years ago • 1 comments

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)));

dciccale avatar Nov 03 '16 08:11 dciccale

However I think if it fails, something has to happen, like a redirect or clean up of the invalid token ?

dciccale avatar Nov 03 '16 08:11 dciccale