utils/src/jwt.ts
ztimson e40f410830
All checks were successful
Build / Build NPM Project (push) Successful in 27s
Build / Tag Version (push) Successful in 7s
Removed window reference from jwtDecode
2024-09-30 16:18:07 -04:00

16 lines
501 B
TypeScript

import {JSONAttemptParse} from './objects.ts';
/**
* Decode a JWT payload, this will not check for tampering so be careful
*
* @param {string} token JWT to decode
* @return {unknown} JWT payload
*/
export function jwtDecode<T>(token: string): T {
const base64 = token.split('.')[1]
.replace(/-/g, '+').replace(/_/g, '/');
return <T>JSONAttemptParse(decodeURIComponent(atob(base64).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join('')));
}