Skip to main content

Decode a JWT token

JavaScript version

const decode = (token) =>
decodeURIComponent(
atob(token.split('.')[1].replace('-', '+').replace('_', '/'))
.split('')
.map((c) => `%${('00' + c.charCodeAt(0).toString(16)).slice(-2)}`)
.join('')
);

TypeScript version

const decode = (token: string): string =>
decodeURIComponent(
atob(token.split('.')[1].replace('-', '+').replace('_', '/'))
.split('')
.map((c) => `%${('00' + c.charCodeAt(0).toString(16)).slice(-2)}`)
.join('')
);

Examples

decode(`
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0I
joxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
`);

// { "sub": "1234567890", "name": "John Doe", "iat": 1516239022 }

Comments