webjwt/ts/index.ts
2022-12-22 17:23:23 +01:00

17 lines
562 B
TypeScript

import * as plugins from './webjwt.plugins.js';
export const getDataFromJwtString = <T = any>(jwtString: string): T => {
const smartenvInstance = new plugins.smartenv.Smartenv();
const splitted = jwtString.split('.');
const dataBase64 = splitted[1];
let plainJsonString: string;
if (smartenvInstance.isBrowser) {
// @ts-ignore
plainJsonString = atob(dataBase64);
} else if (smartenvInstance.isNode) {
let buff = new Buffer(dataBase64, 'base64');
plainJsonString = buff.toString('ascii');
}
return JSON.parse(plainJsonString);
};