# Update user\_metadata example from API docs not working

**URL:** <https://community.auth0.com/t/update-user-metadata-example-from-api-docs-not-working/77656>\
**Category:** Get Help\
**Tags:** user\_metadata, rules\
**Created:** [February 3, 2022, 11:00pm UTC](https://community.auth0.com/t/update-user-metadata-example-from-api-docs-not-working/77656 "2022-02-03T23:00:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![conner.linzy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/conner.linzy/32/33715_2.png) [@conner.linzy](https://community.auth0.com/u/conner.linzy)\
**Post date:** [February 3, 2022, 11:00pm UTC](https://community.auth0.com/t/update-user-metadata-example-from-api-docs-not-working/77656/1 "2022-02-03T23:00:38Z")

</div>

```auto
function(user, context, callback){
  user.user_metadata = user.user_metadata || {};
  // update the user_metadata that will be part of the response
  user.user_metadata.preferences = user.user_metadata.preferences || {};
  user.user_metadata.preferences.fontSize = 12;

  // persist the user_metadata update
  auth0.users.updateUserMetadata(user.user_id, user.user_metadata)
    .then(function(){
      callback(null, user, context);
    })
    .catch(function(err){
      callback(err);
    });
}

```

After many attempts to add metadata to my user object, I decided to paste the example above into my rules (which is directly from the API docs), and it does not seem to work. The `fontSize` property does not appear.

Thus far I have only seen properties added to my user object with this method –

```auto
context.idToken['https://example.com/favorite_color'] =
    user.user_metadata.color;

```

However I do not want the property to be namespaced in that way. I would like it to be named `color`. The properties I want to add are not concerned with idToken.

The first example works when I look at the response in the webtask extension (I can see the user\_metadata field), but it does not manifest in the frontend of my app, only the default fields.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [February 4, 2022, 7:59pm UTC](https://community.auth0.com/t/update-user-metadata-example-from-api-docs-not-working/77656/3 "2022-02-04T19:59:40Z")

</div>

Hi @conner.linzy,

Thanks for reaching out to the Auth0 Community!

First, I have tested the Rule snippet and was able to get it to work. I can confirm the [doc example](https://auth0.com/docs/manage-users/user-accounts/metadata/manage-metadata-rules#update-user-metadata) works correctly without issues.

In this case, I recommend you check the user’s profile details to verify if the _user.user\_metadata.preferences_ has already been created. If so, you will want to delete the property and retry again.

As an alternative, there is also the option to use the Auth0 [Post-Login Action](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow#enrich-the-user-profile) to accomplish the same results. For example:

```auto
exports.onExecutePostLogin = async (event, api) => {
  if(!event.user.user_metadata.preferences){
    const preference = { "fontSize" : 12 }
    api.user.setUserMetadata("preferences", preference);
  }
};

```

Please let me know if you need any clarification or have further questions.

Thank you.

---

<div class="post-metadata">

**Author:** ![pranshushah](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/pranshushah/32/34583_2.png) [@pranshushah](https://community.auth0.com/u/pranshushah)\
**Post date:** [February 4, 2022, 8:01pm UTC](https://community.auth0.com/t/update-user-metadata-example-from-api-docs-not-working/77656/4 "2022-02-04T20:01:12Z")

</div>

first of all, you should try to avoid using rules and use action instead ([why](https://auth0.com/blog/actions-now-generally-available/)) with the action you can do something like this

```auto
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://foo.com';
  const user_metadata = event.user.user_metadata || {}
  api.idToken.setCustomClaim(`${namespace}/user_metadata`,user_metada);
};

```

if you want to know more about auth0 [namespace](https://auth0.com/docs/secure/tokens/json-web-tokens/create-namespaced-custom-claims#:~:text=Use%20the%20following%20guidelines%20for,auth0.com)

daamn me @rueben.tiow wrote the solution at the same time 😅

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [February 4, 2022, 8:19pm UTC](https://community.auth0.com/t/update-user-metadata-example-from-api-docs-not-working/77656/5 "2022-02-04T20:19:57Z")

</div>

Hi @pranshushah!

Thank you for also helping out on this topic! 👏

Yes, I saw you replying at the same time too 😃

---

<div class="post-metadata">

**Author:** ![conner.linzy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/conner.linzy/32/33715_2.png) [@conner.linzy](https://community.auth0.com/u/conner.linzy)\
**Post date:** [February 4, 2022, 11:25pm UTC](https://community.auth0.com/t/update-user-metadata-example-from-api-docs-not-working/77656/6 "2022-02-04T23:25:58Z")

</div>

I will try this out on monday and get back, thanks!

---

<div class="post-metadata">

**Author:** ![conner.linzy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/conner.linzy/32/33715_2.png) [@conner.linzy](https://community.auth0.com/u/conner.linzy)\
**Post date:** [February 7, 2022, 3:46pm UTC](https://community.auth0.com/t/update-user-metadata-example-from-api-docs-not-working/77656/7 "2022-02-07T15:46:43Z")

</div>

Ok I think I am missing a step here. I see the user metadata in the Auth0 User Details tab

 ![image](https://global.discourse-cdn.com/auth0/original/3X/5/3/53140241d69eac60e7c81f7ae7d5b1920fa1738c.png)

My problem is when I call the `user` object from the `useAuth0` hook, I do not see the `user_metadata` prop.

To clarify, my previous definition of “not working” was not being able to access the `user_metadata` prop from `user` after calling fron `useAuth0`

---

<div class="post-metadata">

**Author:** ![conner.linzy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/conner.linzy/32/33715_2.png) [@conner.linzy](https://community.auth0.com/u/conner.linzy)\
**Post date:** [February 7, 2022, 6:36pm UTC](https://community.auth0.com/t/update-user-metadata-example-from-api-docs-not-working/77656/8 "2022-02-07T18:36:54Z")

</div>

I was able to hack this a bit, got this result by adding subfields to a namespaced field. Doesn’t fully solve the issue but can live with it. I’m going to change the name on the frontend and add to my global store and pull from there.

 ![image](https://global.discourse-cdn.com/auth0/original/3X/1/3/1327d63efc841fb25eff0a5f782a13dd0c574b64.png)

```auto
function addPersistenceAttribute(user, context, callback) {
    const mysql = require('mysql');
  
  const connection = mysql.createConnection({
    host: configuration.host,
    user: configuration.user,
    password: configuration.password,
    database: configuration.database
  });
  
  connection.connect();
  const query = `SELECT user_guid, email_verified, active, user_type, last_online , subscription_level FROM users_v2 WHERE email = "${user.email}"`;
  
  connection.query(query, [user.email], function(err, results) {
    if (err || results.length === 0) return callback(err || null);

    const dbUser = results[0];
    
    user.user_metadata = user.user_metadata || {};
    user.user_metadata.user_guid = user.user_metadata.user_guid || dbUser.user_guid.toString();
    user.user_metadata.email_verified = user.user_metadata.email_verified || dbUser.email_verified;
    user.user_metadata.active = user.user_metadata.active || dbUser.active;
    user.user_metadata.user_type = user.user_metadata.user_type || dbUser.user_type;
    user.user_metadata.last_online = user.user_metadata.last_online || dbUser.last_online;
    user.user_metadata.subscription_level = user.user_metadata.subscription_level || dbUser.subscription_level;

  context.idToken['https://example.com/user_metadata'] = { ...user.user_metadata };

  auth0.users
    .updateUserMetadata(user.user_id, user.user_metadata)
    .then(function () {
      callback(null, user, context);
    })
    .catch(function (err) {
      callback(err);
    });
  });
}

```

Please do not close the issue! I’d like to know what I am doing wrong.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [February 9, 2022, 8:33pm UTC](https://community.auth0.com/t/update-user-metadata-example-from-api-docs-not-working/77656/9 "2022-02-09T20:33:44Z")

</div>

Hi @conner.linzy,

Thank you for your response.

Having looked closely at your screenshots, it appears that your Rule is updating the user’s user\_metadata and appending them as custom claims correctly. Everything seems fine.

> [@conner.linzy](#):
>
> I’d like to know what I am doing wrong.

Could you please clarify the issues you are experiencing now?

Thanks.

---

<div class="post-metadata">

**Author:** ![conner.linzy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/conner.linzy/32/33715_2.png) [@conner.linzy](https://community.auth0.com/u/conner.linzy)\
**Post date:** [February 10, 2022, 10:33pm UTC](https://community.auth0.com/t/update-user-metadata-example-from-api-docs-not-working/77656/10 "2022-02-10T22:33:35Z")

</div>

Sure, I dont want to use the namespaced claim. When I access the user\_metadata from the Auth0 hook on the frontend I currently need to access it like this:

`user['https://example.com/user_metadata'].user_guid`

I would prefer to do something like this

`user.user_metadata.user_guid`

I’m trying to understand why the first example in this thread cannot do that, and why I must use this namespaced field (`context.idToken[...`). It looks messy, sucks to type, and I will probably have to explain it to my teammates in the future because it is a bit weird.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [February 24, 2022, 5:58pm UTC](https://community.auth0.com/t/update-user-metadata-example-from-api-docs-not-working/77656/11 "2022-02-24T17:58:34Z")

</div>

Hi @conner.linzy,

Thank you for your response.

> [@conner.linzy](#):
>
> I would prefer to do something like this
> 
> `user.user_metadata.user_guid`

Unfortunately, this is not possible with the [`useAuth0` hook’s](https://auth0.github.io/auth0-react/modules/use_auth0.html)`getIdTokenClaims` method because it is used to get the claims from the ID Token. In this scenario, the recommendation of appending them to a namespaced claim is the **only** way to get the user’s user\_metadata in the ID token.

As an alternative, you could consider using the [Management API get a user endpoint](https://auth0.com/docs/api/management/v2#!/Users/get_users_by_id) in conjunction with the Post-Login action mentioned [here](https://community.auth0.com/t/update-user-metadata-example-from-api-docs-not-working/77656/3). This way, you can get the user\_metadata calling `user.user_metadata.user_guid` instead of calling the user\_metadata as a custom namespaced claim.

Hoped this helps!

Please let me know if you need further clarification or have any questions.

Thank you.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 11, 2022, 5:58pm UTC](https://community.auth0.com/t/update-user-metadata-example-from-api-docs-not-working/77656/12 "2022-03-11T17:58:41Z")

</div>

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