# Can't Assign Roles to users With actions and management API

**URL:** <https://community.auth0.com/t/cant-assign-roles-to-users-with-actions-and-management-api/121333>\
**Category:** Get Help\
**Tags:** new-universal-login-experience, login-experience\
**Created:** [November 21, 2023, 1:02pm UTC](https://community.auth0.com/t/cant-assign-roles-to-users-with-actions-and-management-api/121333 "2023-11-21T13:02:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rezgui.aziz](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rezgui.aziz/32/51642_2.png) [@rezgui.aziz](https://community.auth0.com/u/rezgui.aziz)\
**Post date:** [November 21, 2023, 1:02pm UTC](https://community.auth0.com/t/cant-assign-roles-to-users-with-actions-and-management-api/121333/1 "2023-11-21T13:02:04Z")

</div>

This is the code :

```auto
exports.onExecutePostLogin = async (event, api) => {

  const ManagementClient = require('auth0').ManagementClient;

  const management = new ManagementClient({
    domain: event.secrets.domain,
    clientId: event.secrets.clientId,
    clientSecret: event.secrets.clientSecret,
  });

  const params = { id: event.user.user_id };
  const data = { "roles": ["ROLE_ID"] };

  try {
    const res = await management.assignRolestoUser(params, data)
  } catch (e) {
    console.log(e)
    // Handle error
  }
};

```

This Is the Error :

```auto
TypeError: management.assignRolestoUser is not a function

```

I took this code from an old post, so I taught the API changed , but I could not figure out how to do it, I guess it have something to do with [UserManager:](https://auth0.github.io/node-auth0/classes/management.UsersManager.html)

---

<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:** [November 21, 2023, 4:53pm UTC](https://community.auth0.com/t/cant-assign-roles-to-users-with-actions-and-management-api/121333/3 "2023-11-21T16:53:05Z")

</div>

Hi @rezgui.aziz,

Recently, there was an update to the ManagementClient API. As part of this update, some of the methods underwent name changes.

The revised call to assign roles to users is now `management.users.assignRoles(params, data)`.

(Reference: [RolesManager | auth0](https://auth0.github.io/node-auth0/classes/management.RolesManager.html#assignUsers))

After implementing these updates in your Post Login action script, you should be able to assign roles to your users without any issues.

Please reach out if you have any issues with this.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![rezgui.aziz](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rezgui.aziz/32/51642_2.png) [@rezgui.aziz](https://community.auth0.com/u/rezgui.aziz)\
**Post date:** [November 22, 2023, 9:31am UTC](https://community.auth0.com/t/cant-assign-roles-to-users-with-actions-and-management-api/121333/4 "2023-11-22T09:31:15Z")

</div>

Hey Rueben, thanks

```auto
const res = await management.users.assignRoles(params, data)

```

this worked for me.  
Now in my next app I need to set up protected routes for the admin, and I want him to be able to see users and delete some of them, please help me out.  
Thanks.

---

<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:** [November 28, 2023, 8:02pm UTC](https://community.auth0.com/t/cant-assign-roles-to-users-with-actions-and-management-api/121333/5 "2023-11-28T20:02:53Z")

</div>

Hi @rezgui.aziz,

Thanks for the reply.

You could set up protected routes by requiring authentication to access the resource.

For an example, check out [this example](https://auth0.com/docs/quickstart/webapp/express/01-login#display-user-profile) using the Auth0 Express SDK that shows how to protect a route.

Then, you could use the Management API to display and delete the users accordingly.

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:** [December 12, 2023, 8:03pm UTC](https://community.auth0.com/t/cant-assign-roles-to-users-with-actions-and-management-api/121333/6 "2023-12-12T20:03:48Z")

</div>

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