How can I retrieve the email of an authenticated user in html?

Hi Community!

I’m very new to code and auth0 as well.

I’ve successfully implemented login and redirect to an application page, but I don’t know how to retrieve the email address (from the header data?) of an authenticated user and display it.

Can someone please help? Thank you for any direction you can suggest!

  • Which SDK this is regarding: - auth0-spa-js.js (from CDN)
  • Platform Version: using just javascript and HTML

Hi @dannywinnick,

You may find the JavaScript Quickstart helpful: Auth0 JavaScript SDK Quickstarts: Login

As shown in the sample app, you can get the user info like so:

/**
 * Updates the user interface
 */
const updateUI = async () => {
  try {
    const isAuthenticated = await auth0.isAuthenticated();

    if (isAuthenticated) {
      const user = await auth0.getUser();

      document.getElementById("profile-data").innerText = JSON.stringify(
        user,
        null,
        2
      );

      document.querySelectorAll("pre code").forEach(hljs.highlightBlock);

      eachElement(".profile-image", (e) => (e.src = user.picture));
      eachElement(".user-name", (e) => (e.innerText = user.name));
      eachElement(".user-email", (e) => (e.innerText = user.email));
      eachElement(".auth-invisible", (e) => e.classList.add("hidden"));
      eachElement(".auth-visible", (e) => e.classList.remove("hidden"));
    } else {
      eachElement(".auth-invisible", (e) => e.classList.remove("hidden"));
      eachElement(".auth-visible", (e) => e.classList.add("hidden"));
    }
  } catch (err) {
    console.log("Error updating UI!", err);
    return;
  }

  console.log("UI updated");
};

The code above updates the UI with profile info for the logged-in user.

Thanks so much, I’ll try this and report back!

1 Like