@lfai/egeria-js-commons
Version:
Common module for storing static data such as key value objects, SVGs, icon mappings, API urls.
35 lines (34 loc) • 1 kB
JavaScript
export const parseJwt = (token) => {
if (token !== null && token !== "") {
try {
const base64Url = token.split('.')[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
}
catch (e) {
return null;
}
}
return null;
};
export const token = {
setValue: (value) => {
localStorage.setItem('token', value);
},
getValue: () => {
return localStorage.getItem('token') || null;
},
delete: () => {
localStorage.removeItem('token');
},
decodedObject: function () {
const decodedObject = parseJwt(this.getValue());
if (decodedObject) {
return decodedObject;
}
return null;
}
};