Added jwtDecode function
This commit is contained in:
parent
e6636d373b
commit
67d9928a61
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.16.3",
|
"version": "0.16.7",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.16.3",
|
"version": "0.16.7",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^29.5.12",
|
"@types/jest": "^29.5.12",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.16.6",
|
"version": "0.16.7",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -6,6 +6,7 @@ export * from './files';
|
|||||||
export * from './emitter';
|
export * from './emitter';
|
||||||
export * from './errors';
|
export * from './errors';
|
||||||
export * from './http';
|
export * from './http';
|
||||||
|
export * from './jwt';
|
||||||
export * from './logger';
|
export * from './logger';
|
||||||
export * from './math';
|
export * from './math';
|
||||||
export * from './misc';
|
export * from './misc';
|
||||||
|
15
src/jwt.ts
Normal file
15
src/jwt.ts
Normal 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('')));
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user