webjwt/ts/index.ts

17 lines
562 B
TypeScript
Raw Permalink Normal View History

2022-12-22 16:12:15 +00:00
import * as plugins from './webjwt.plugins.js';
export const getDataFromJwtString = <T = any>(jwtString: string): T => {
2022-12-22 16:21:43 +00:00
const smartenvInstance = new plugins.smartenv.Smartenv();
2022-12-22 16:12:15 +00:00
const splitted = jwtString.split('.');
const dataBase64 = splitted[1];
2022-12-22 16:21:43 +00:00
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);
2022-12-22 16:23:23 +00:00
};