# Cannot set data to APP\_METADATA.ROLES

**URL:** <https://community.auth0.com/t/cannot-set-data-to-app-metadata-roles/104209>\
**Category:** Get Help\
**Tags:** actions-management-api, management-api\
**Created:** [April 5, 2023, 8:29pm UTC](https://community.auth0.com/t/cannot-set-data-to-app-metadata-roles/104209 "2023-04-05T20:29:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dev54](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dev54/32/46016_2.png) [@dev54](https://community.auth0.com/u/dev54)\
**Post date:** [April 5, 2023, 8:29pm UTC](https://community.auth0.com/t/cannot-set-data-to-app-metadata-roles/104209/1 "2023-04-05T20:29:13Z")

</div>

Hey guys im trying to set data to the `app_metadata.authorization.roles` using flows post-login and post-registration.

So far Im assigning the roles created using `management.assignRolestoUser`, I assumed this method would auto add the role in question to the `app_metadata.authorization.roles`, but it does not.

Im using this code to set the roles using a post-login. it works for the first time the user logs in, but if I use the management api else where to update user metadata, the `app_metadata.authorization.roles` resets automatically to an empty array.

`app_metadata.authorization.roles=["ROLENAME"]` to `app_metadata.authorization.roles=[]`

code im using.

```auto
exports.onExecutePostUserRegistration = async (event, api) => {
const ManagementClient = require(‘auth0’).ManagementClient;
const management = new ManagementClient({
domain: String(event.secrets.DOMAIN),
clientId: String(event.secrets.ID),
clientSecret: String(event.secrets.SECRET),
scope: String(event.secrets.SCOPE),
audience: String(event.secrets.AUDIENCE),
});

const params = { id: event.user.user_id };
const data = { “roles”: [String(event.secrets.ROLEUSER)] };
const userDetails = {
app_metadata: {
roles: [“UNIQUEROLENAME”],
},
};
try {
const res = await management.assignRolestoUser(params, data);
// assign role details to app_metadata
const res2 = await management.updateUser(params, userDetails);
} catch (e) {
console.log(e)
}
};

```

---

<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 7, 2023, 3:04am UTC](https://community.auth0.com/t/cannot-set-data-to-app-metadata-roles/104209/3 "2023-04-07T03:04:20Z")

</div>

Hi @dev54,

Thanks for reaching out to the Auth0 Community!

> [@dev54](#):
>
> So far Im assigning the roles created using `management.assignRolestoUser`, I assumed this method would auto add the role in question to the `app_metadata.authorization.roles`, but it does not.

Unfortunately, the `management.assignRolestoUser` method only assigns the Roles to the user and does not automatically assign them to the app\_metadata.

To set the app\_metadata inside a Post-Login Action, please call the `api.user.setUserMetadata(name, value)` method. Please note that the API object is not callable in a Post-User Registration Action.

(Reference: [Actions Triggers: post-login - API Object](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/api-object#:~:text=api.user.setUserMetadata(name%2C%20value)))

For example:

```auto
exports.onExecutePostUserRegistration = async (event, api) => {
  const ManagementClient = require(‘auth0’).ManagementClient;
  const management = new ManagementClient({
    domain: String(event.secrets.DOMAIN),
    clientId: String(event.secrets.ID),
    clientSecret: String(event.secrets.SECRET),
    scope: String(event.secrets.SCOPE),
    audience: String(event.secrets.AUDIENCE),
  });

  const params = { id: event.user.user_id };
  const data = { “roles”: [String(event.secrets.ROLEUSER)] };
  const userDetails = {
    app_metadata: {
      roles: [“UNIQUEROLENAME”],
    },
  };
  try {
    const res = await management.assignRolestoUser(params, data);
    // assign role details to app_metadata
    api.user.setUserMetadata("roles", "UNIQUEROLENAME")
  } catch (e) {
    console.log(e)
  }
};

```

Let me add that this can also be accomplished by calling the ManagementClient [`updateAppMetadata`](https://auth0.github.io/node-auth0/UsersManager.html#updateAppMetadata) method if preferred.

I hope this helps!

Please reach out again if you have any further questions.

Thanks,  
Rueben

---

<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:** [April 21, 2023, 3:04am UTC](https://community.auth0.com/t/cannot-set-data-to-app-metadata-roles/104209/4 "2023-04-21T03:04:59Z")

</div>

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