cf-auth0
Version:
🔐 Auth0 Client on Cloudflare Pages
25 lines (24 loc) • 764 B
JavaScript
import { jwsDecode } from './jws/lib/jwsDecode';
export function decode(jwt, options = {}) {
const decoded = jwsDecode(jwt, options);
const { header, signature } = decoded;
let { payload } = decoded;
//try parse the payload
if (typeof payload === 'string') {
const obj = JSON.parse(payload);
if (obj !== null && typeof obj === 'object') {
payload = obj;
}
}
//return header if `complete` option is enabled. header includes claims
//such as `kid` and `alg` used to select the key within a JWKS needed to
//verify the signature
if (options.complete === true) {
return {
header,
payload: payload,
signature
};
}
return payload;
}