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

495 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

  • Enable Firewall Rules and a JavaScript Browser Challenge 2247 16
  • Enable Cloudflare Rate Limiting 2134 0
  • How to Add a JavaScript Snippet 1994 8
  • IP Allowlist 1945 0
  • Signing up for Cloudflare and Configuring Your Domain 1812 4

New Articles

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