Added jwtDecode function
All checks were successful
Build / Build NPM Project (push) Successful in 31s
Build / Tag Version (push) Successful in 6s

This commit is contained in:
2024-09-30 15:58:37 -04:00
parent e6636d373b
commit 67d9928a61
4 changed files with 19 additions and 3 deletions

15
src/jwt.ts Normal file
View File

@ -0,0 +1,15 @@
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
*/
function decodeJwt<T>(token: string): T {
const base64 = token.split('.')[1]
.replace(/-/g, '+').replace(/_/g, '/');
return <T>JSONAttemptParse(decodeURIComponent(window.atob(base64).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join('')));
}