react-oauth2-code-pkce
Version:
Provider agnostic react package for OAuth2 Authorization Code flow with PKCE
46 lines (45 loc) • 1.49 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.decodeIdToken = exports.decodeAccessToken = exports.decodeJWT = void 0;
/**
* Decodes the base64 encoded JWT. Returns a TToken.
*/
const decodeJWT = (token) => {
try {
const base64Url = token.split('.')[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const jsonPayload = decodeURIComponent(atob(base64)
.split('')
.map((c) => `%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}`)
.join(''));
return JSON.parse(jsonPayload);
}
catch (e) {
console.error(e);
throw Error('Failed to decode the access token.\n\tIs it a proper JSON Web Token?\n\t' +
"You can disable JWT decoding by setting the 'decodeToken' value to 'false' the configuration.");
}
};
exports.decodeJWT = decodeJWT;
const decodeAccessToken = (token) => {
if (!token || !token.length)
return undefined;
try {
return (0, exports.decodeJWT)(token);
}
catch (e) {
console.warn(`Failed to decode access token: ${e.message}`);
}
};
exports.decodeAccessToken = decodeAccessToken;
const decodeIdToken = (idToken) => {
if (!idToken || !idToken.length)
return undefined;
try {
return (0, exports.decodeJWT)(idToken);
}
catch (e) {
console.warn(`Failed to decode idToken: ${e.message}`);
}
};
exports.decodeIdToken = decodeIdToken;
;