Skip to content

Commit

Permalink
resolve #4
Browse files Browse the repository at this point in the history
The problem is that the Base64 in JWT token is being compressed, which means there is no padding on the encoded string. That will cause error when trying to decode it using `atob()`.

Resolve that by adding a [function](https://github.com/Kunniii/edn_auto_ext/blob/v2/src/ready.js#L48) named `addPaddingToBase64()` and use it to add padding before converting it to a string.
  • Loading branch information
Kunniii committed May 18, 2023
1 parent cc46b31 commit 0621a1e
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@ function showIndicate(text, color, seconds) {
}, 1000 * seconds);
}

let token = localStorage.getItem("token");
let myEmail;
try {
myEmail = JSON.parse(atob(token.split(".")[1])).email;
} catch {
myEmail = "Bạn";
function addPaddingToBase64(base64String) {
while (base64String.length % 4 !== 0) {
base64String += "=";
}
return base64String;
}

let token = localStorage.getItem("token");
let b64 = addPaddingToBase64(token.split(".")[1]);
let myEmail = JSON.parse(atob(b64)).email;

const API = "https://fugw-edunext.fpt.edu.vn:8443/api/v1";
// const API = "https://fugw-edunext.fpt.edu.vn/api/v1";

Expand Down Expand Up @@ -80,4 +84,4 @@ const options = {
},
};

showIndicate(`Welcome ${myEmail.split("@")[0]}`, "#35a661", 1.5);
showIndicate(`Welcome ${myEmail.split("@")[0]}`, "#35a661", 1);

0 comments on commit 0621a1e

Please sign in to comment.