commercebuild Success Portal commercebuild Success Portal

  • Home
  • Submit a Ticket
  • Updates
  • Login
  • Contact Us
Home / Tutorials / How to Get User Email and User Name on the Web Store Front End

How to Get User Email and User Name on the Web Store Front End

922 views Less than a minute 0

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; 
} 
Tutorials

About Bradly Hale

View all posts by Bradly Hale →

Related Articles

  • How to Enable reCAPTCHA on Custom Forms
  • Adding a custom message to the shipping step at checkout
  • How to configure Stripe as a payment method
  • SQL Server Change Tracking Query for Sage 300 and X3

Popular Articles

  • How to Delete or Deactivate Promo Codes 3720 0
  • How to Generate Sitemaps and the Google Product Feed 3629 0
  • Enable Cloudflare Rate Limiting 3071 0
  • Enable Firewall Rules and a JavaScript Browser Challenge 2865 16
  • IP Allowlist 2611 0

New Articles

  • Error when adding a new user: Email is already in use 1094 0
  • How to Check commercebuild Payments Version 1384 0
  • How to Enable reCAPTCHA on Custom Forms 1239 0
  • Manually posting an order 1460 0
  • Adding a custom message to the shipping step at checkout 1252 0
© commercebuild 2022. All rights reserved.