JWT decoding working in React Native but not for React

14,255

You can use any npm module for decoding like :

npm install jwt-decode

var jwtDecode = require('jwt-decode');

var token = 'eyJ0eXAiO.../// jwt token';

var decoded = jwtDecode(token);
console.log(decoded);

For further info check here

Share:
14,255
Leo
Author by

Leo

Updated on June 04, 2022

Comments

  • Leo
    Leo almost 2 years

    So when you decode the following in https://jwt.io/ it says it has a header named 'token' which has the token when you re-decode it you can access the payload.

    Normally in RN I can access the payload containing token using this.props.user.token but in React the 'token' property is not there as it is not an javascript object but a string but I use the same API to my backend.

    eyJ0b2tlbiI6ImV5SmhiR2NpT2lKSVV6STFOaUlzSW5SNWNDSTZJa3BYVkNKOS5leUpwWkhCaGMzTWlPaUl6WkRFME1ERmlNQzFtTkdWakxURXhaVGN0WVdReE15MHpaRGRsTUdZME9EUmlaR0k2TVRsaU4yTmpNREUyWVRVeU1ERTRNemt3TmpsbU56aGxNV0pqTXpBMk4yUTBZalJqTVRrNE1HTTBPV05tWkRsak9XWTRNV1F4Tm1VMllUbGhaR1JrTVNJc0ltbGhkQ0k2TVRVeE5UY3lNalF6TkgwLlg0dGUxRmhzbGJlcW5Id3RCQ2NOMHF5Zk9vb2tvdVN2TXRlLVBUTUtrMHcifQ==
    

    I tried to decode it using

    var decoded = jwt.decode(response.data);
    

    but decoded is null, what can I do?

  • Leo
    Leo over 6 years
    I told you it's not working for my case you still reply with the same code
  • yogesh agrawal
    yogesh agrawal over 6 years
  • Chandni
    Chandni over 5 years
    is any liabrary which is compatible with this for react native mobile application