# How to attach values to user metadata on automatic migration?

**URL:** <https://community.auth0.com/t/how-to-attach-values-to-user-metadata-on-automatic-migration/81287>\
**Category:** Get Help\
**Tags:** user\_metadata, migration\
**Created:** [April 6, 2022, 5:55pm UTC](https://community.auth0.com/t/how-to-attach-values-to-user-metadata-on-automatic-migration/81287 "2022-04-06T17:55:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudumonteiro](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dudumonteiro/32/35891_2.png) [@dudumonteiro](https://community.auth0.com/u/dudumonteiro)\
**Post date:** [April 6, 2022, 5:55pm UTC](https://community.auth0.com/t/how-to-attach-values-to-user-metadata-on-automatic-migration/81287/1 "2022-04-06T17:55:24Z")

</div>

I am currently running an auth0 automatic migration proccess.  
When user logs in, the following script is ran by auth0:

```javascript
function login(username, password, callback) {
  const request = require('request');
  const namespace = ' *****';
  
  request.get({
    url: 'https:// ******** /api/profile',
    auth: {
      username: username,
      password: password
    }
    //for more options check:
    //https://github.com/mikeal/request#requestoptions-callback
  }, function(err, response, body) {
    if (err) return callback(err);
    if (response.statusCode === 401) return callback();
    const user = JSON.parse(body);

    callback(null, {
      user_id: user.user_name.toString(),
      email: user.email,
      first_name: user.first_name,
      last_name: user.last_name,
      position: user.position,
      organization: user.organization,
      phone_number: user.phone_number,
      icon_url: user.icon_url
    });
  });
}

```

The problem is, I can only attach fields to idtoken only from user\_metadata (or app\_metadata), and those fields set on post-login script showed above aren’t set on user\_metadata.

My question is: How do I set those fields to user\_metadata on that script?

---

<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:** [April 6, 2022, 11:03pm UTC](https://community.auth0.com/t/how-to-attach-values-to-user-metadata-on-automatic-migration/81287/3 "2022-04-06T23:03:28Z")

</div>

Hi @dudumonteiro,

Thanks for reaching out to the Auth0 Community!

I understand that you would like to append data to the user’s user\_metadata automatically when they migrate.

Since users that automatically migrate need to log in at least once, I recommend using a Post-Login Action script to add user\_metadata to the user’s profile.

For example:

```auto
/**
 * @param {Event} event - Details about the user and the context in which they are logging in.
 * @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
 */
exports.onExecutePostLogin = async (event, api) => {
  api.user.setUserMetadata("favorite_color", "blue");
};

```

> **[Login Flow](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow#enrich-the-user-profile)**
>
> Learn about the Actions Login Flow and the post-login Action trigger, which is executed after a user logs in and when a Refresh Token is requested.

Hoped this helps!

Please do let me know if you have any additional questions.

Thank you.

---

<div class="post-metadata">

**Author:** ![dudumonteiro](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dudumonteiro/32/35891_2.png) [@dudumonteiro](https://community.auth0.com/u/dudumonteiro)\
**Post date:** [April 7, 2022, 10:41am UTC](https://community.auth0.com/t/how-to-attach-values-to-user-metadata-on-automatic-migration/81287/4 "2022-04-07T10:41:55Z")

</div>

Hi @rueben.tiow ,  
Thanks for replying.

Unfortunately, I have already tested that Post-Login Action, and didn´t work. I need an actual value from migrated user, not a constant, like “blue”.

```auto
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https:// *********';
  const p_org = event.user.organization; // it seems this value is null but it appears on auth0 dashboard

  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/user_metadata.organization`, p_org);    
  }
};

```

---

<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:** [April 11, 2022, 6:46pm UTC](https://community.auth0.com/t/how-to-attach-values-to-user-metadata-on-automatic-migration/81287/5 "2022-04-11T18:46:32Z")

</div>

Hi @dudumonteiro,

Thank you for your response.

> [@dudumonteiro](#):
>
> Unfortunately, I have already tested that Post-Login Action, and didn´t work.

Have you made sure that your namespaces follow the guidelines explained in our [Create Custom Claims](https://auth0.com/docs/secure/tokens/json-web-tokens/create-namespaced-custom-claims) documentation?

Moreover, the `event.user.organization` is not a callable property. Instead, use the `event.organization` property in your Action script.

See [here](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/event-object) for the complete list of Event Objects callable in a Post Login Action.

Please let me know how this goes.

Thank you.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [December 21, 2022, 5:15pm UTC](https://community.auth0.com/t/how-to-attach-values-to-user-metadata-on-automatic-migration/81287/6 "2022-12-21T17:15:51Z")

</div>


