Created
March 3, 2020 12:39
-
-
Save AntsiferovMaxim/db57533d206e203b4d10763142abb96b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const atob = window.atob; | |
function b64DecodeUnicode(str) { | |
return decodeURIComponent(atob(str).replace(/(.)/g, function (m, p) { | |
let code = p.charCodeAt(0).toString(16).toUpperCase(); | |
if (code.length < 2) { | |
code = '0' + code; | |
} | |
return '%' + code; | |
})); | |
} | |
function base64UrlDecode(str) { | |
let output = str.replace(/-/g, "+").replace(/_/g, "/"); | |
switch (output.length % 4) { | |
case 0: | |
break; | |
case 2: | |
output += "=="; | |
break; | |
case 3: | |
output += "="; | |
break; | |
default: | |
throw "Illegal base64url string!"; | |
} | |
try{ | |
return b64DecodeUnicode(output); | |
} catch (err) { | |
return atob(output); | |
} | |
} | |
export function jwtDecode(token) { | |
const payloadPosition = 1; | |
return JSON.parse(base64UrlDecode(token.split('.')[payloadPosition])) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment