Post login action not setting user metadata

Hello,

I’m trying to use a post-login action to set a picture property within the user metadata. I’ve created and deployed the following action:

exports.onExecutePostLogin = async (event, api) => {
  api.user.setUserMetadata("picture", "https://db3pap007files.storage.live.com/y4mJQhpcRBjwDJkh5aIVjH95pD-085UxR5r1OyQX2-mNOCvqyjghcHimd6KXwSL2W5YeP_AhLN5r4GJYnfcm0SrJowroK4xAh0c8iNWuAiApICBJ9keaiIlAko19bkBqEIQnxJhJY2sQ6LktRppQJLaIaslG5gP9XqECzNqqAUaaxXE5DmBFzu_e3uwX4JebmF0?width=1200&height=1599&cropmode=none");
};

I then incorporated this custom action into the Login flow, between the Start and Complete stages:

However, when I log into my SPA the getUser() method on the auth0 client, I’m not getting the user metadata:

const user = await auth0Client.getUser();

After this call, the user variable contains the basic attributes, e.g. nickname, name, email, etc., but does not have a user metadata attribute.

Can anyone say what I’m doing wrong here or why the user metadata is not being returned?

I noticed that, in the template app from which I created my SPA, the call above was following by this comment:

      // Here you should extract the complete user profile to make it available in your entire app.
      // The auth state only provides basic information.

Is it the case that the getUser() method only returns a subset of the user profile, and if so, how can I get the complete profile?

Hi @paultipper,

Welcome to the Auth0 Community!

I understand that you have questions regarding user metadata.

First, I’d like to confirm that your Action works correctly and sets the picture in the user’s metadata.

Next, that’s correct. The const user = await auth0Client.getUser(); returns the basic user profile attributes. You can see the full list here: https://auth0.github.io/auth0-spa-js/classes/user.html, which does not include the user_metadata property.

In this situation, I recommend that you get the user’s email with .getUser() and make a call to the Management API to the /get_users_by_email endpoint.

After doing so, you can filter for the user’s metadata from the results returned from the user profile as a JSON object.

Let me know if this addresses your problem.

Thank you.

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.