For some front end JavaScript that may run on a web store, accessing the user name or user email may be helpful to satisfy certain parameter requirements for those scripts. Fortunately, this information is accessible via the web store userId cookie.
The following snippet will help you access these data:
let fetchCookie = getCookie("userId");
let jwtTokenDetails = parseJwt(fetchCookie);
// Returns the email address of the user
var email = jwtTokenDetails.user_name;
// Returns the user ID
var userID = jwtTokenDetails.user_id;
// Decodes the JWT token
function parseJwt(token) {
try {
return JSON.parse(atob(token.split('.')[1]));
} catch (e) {
return null;
}
};
function getCookie(name) {
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}