Friday, December 23, 2022

How to decode JWT Token

 function parseJwt (token) {

    var base64Url = token.split('.')[1];

    var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');

    var jsonPayload = decodeURIComponent(window.atob(base64).split('').map(function(c) {

        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);

    }).join(''));


    return JSON.parse(jsonPayload);

}


this is usually done by library such as jwt, but with this, it can be done without that


references:

https://stackoverflow.com/questions/38552003/how-to-decode-jwt-token-in-javascript-without-using-a-library

No comments:

Post a Comment