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

This is the code :

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 :

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:

1 Like

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)

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

Hey Rueben, thanks

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.

1 Like

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 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

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