Can't assign Default Roles to a User on Sign up Using Auth0 Actions

My code:

exports.onExecutePostLogin = async (event, api) => {
   if (event.authorization && event.authorization.roles && event.authorization.roles.length > 0) {
    return;
  }
  // Create management API client instance
  const { ManagementClient } = require("auth0"); 
  // or use : const ManagementClient = require("auth0").ManagementClient;
  // also get same error.

  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" : [event.secrets.default_roles] };

  try {
    await management.users.assignRoles(params, data);
  } catch (e) {
    console.log(`Error assigning default role: ${e.message}`);
  }
};

Dependency: auth0@latest
All variable environment created successful.

Node v22

This is the error:

Error assigning default role: management.users.assignRoles is not a function

I learned code from youtube: Assigning Default Roles to a User on Sign up Using Auth0 Actions. But I couldn’t make. Help me please!

Hi @loihnt05 and welcome to the Auth0 Community!

Please allow me some time to look into your issue, and I will get back to you as soon as possible.

Thanks!
Teodor.

1 Like

Hi @loihnt05

Welcome to the Auth0 Community!

The reason you receive this error is that the users object doesn’t contain an assignRoles method. In the link below, you’ll find the documentation on how to assign roles to users.

If you have any other questions, feel free ot reach out.

Have a good one,
Vlad

1 Like