Default Role (Native Apps)

Hello guys, This is my first time using auth0. I created a native application, and I’m using actions to add roles to idToken object, etc. But, I can’t find a way to add default roles with Native applications for new users or users without any roles. So, this is the trigger to add default roles.

exports.onExecutePostLogin = async (event, api) => {
  if (event.authorization && event.client.client_id === event.secrets.CLIENT_ID && event.authorization.roles && event.authorization.roles.length > 0) {
    return;
  }

  // Create management API client instance
  const ManagementClient = require("auth0").ManagementClient;

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

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

  try {
    await management.users.assignRoles(params, data);
  } catch (e) {
    console.log(e);
  }
};

I got an error: AuthApiError: Grant type 'client_credentials' not allowed for the client.

After that I noticed I need to have a M2M application, so I created one and updated the secret values, and then I got a different error:

ManagementApiError: Invalid token
    at UsersManager.parseError (<node_modules>/auth0/dist/cjs/management/management-client.js:33:16)

I’d appreciate your help on this.

Hi @fhenriquez

Welcome to the Auth0 Community!

I think the issue may be that your M2M application is not assigned
to your application on your Management API > Machine to Machine Applications > Auth0 Management API (Test Application) for example:

Here’s link to the thread with further explanation:

Thanks
Dawid

Awesome, thank you so much.