Skip to content

Instantly share code, notes, and snippets.

@catwhocode
Created April 20, 2022 04:59
Show Gist options
  • Save catwhocode/d7d2b7904ed43facf2cd71e2dbe500d1 to your computer and use it in GitHub Desktop.
Save catwhocode/d7d2b7904ed43facf2cd71e2dbe500d1 to your computer and use it in GitHub Desktop.
JS: Decode JWT without library
/* taken from: */
/* https://stackoverflow.com/questions/38552003/how-to-decode-jwt-token-in-javascript-without-using-a-library?noredirect=1&lq=1 */
const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImp0aSI6ImU3YjQ0Mjc4LTZlZDYtNDJlZC05MTZmLWFjZDQzNzhkM2U0YSIsImlhdCI6MTU5NTg3NzUxOCwiZXhwIjoxNTk1ODgxMTE4fQ.WXyDlDMMSJAjOFF9oAU9JrRHg2wio-WolWAkAaY3kg4';
const tokenDecodablePart = token.split('.')[1];
const decoded = Buffer.from(tokenDecodablePart, 'base64').toString();
console.log(decoded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment