decode.js
1 var jws = require('jws'); 2 3 module.exports = function (jwt, options) { 4 options = options || {}; 5 var decoded = jws.decode(jwt, options); 6 if (!decoded) { return null; } 7 var payload = decoded.payload; 8 9 //try parse the payload 10 if(typeof payload === 'string') { 11 try { 12 var obj = JSON.parse(payload); 13 if(obj !== null && typeof obj === 'object') { 14 payload = obj; 15 } 16 } catch (e) { } 17 } 18 19 //return header if `complete` option is enabled. header includes claims 20 //such as `kid` and `alg` used to select the key within a JWKS needed to 21 //verify the signature 22 if (options.complete === true) { 23 return { 24 header: decoded.header, 25 payload: payload, 26 signature: decoded.signature 27 }; 28 } 29 return payload; 30 };