# Default Role (Native Apps)

**URL:** <https://community.auth0.com/t/default-role-native-apps/187124>\
**Category:** Get Help\
**Tags:** configuration, application\
**Created:** [May 23, 2025, 4:22pm UTC](https://community.auth0.com/t/default-role-native-apps/187124 "2025-05-23T16:22:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fhenriquez](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/fhenriquez/32/69023_2.png) [@fhenriquez](https://community.auth0.com/u/fhenriquez)\
**Post date:** [May 23, 2025, 4:22pm UTC](https://community.auth0.com/t/default-role-native-apps/187124/1 "2025-05-23T16:22:23Z")

</div>

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.

```auto
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:

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

```

I’d appreciate your help on this.

---

<div class="post-metadata">

**Author:** ![dawid.matuszczyk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dawid.matuszczyk/32/74550_2.png) [@dawid.matuszczyk](https://community.auth0.com/u/dawid.matuszczyk)\
**Post date:** [May 23, 2025, 5:07pm UTC](https://community.auth0.com/t/default-role-native-apps/187124/4 "2025-05-23T17:07:22Z")

</div>

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:

 ![Screenshot 2025-05-23 at 19.05.25](https://global.discourse-cdn.com/auth0/original/3X/b/9/b986b7261d5c9c90c2ae3edac7bd0a8fc3e17baf.png)

Here’s link to the thread with further explanation:

- [Add a Default Role to a New User on First Login](https://community.auth0.com/t/add-a-default-role-to-a-new-user-on-first-login/25857)
- [Get Auth0 Management API invalid token error](https://community.auth0.com/t/get-auth0-management-api-invalid-token-error/83649)

Thanks  
Dawid

---

<div class="post-metadata">

**Author:** ![fhenriquez](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/fhenriquez/32/69023_2.png) [@fhenriquez](https://community.auth0.com/u/fhenriquez)\
**Post date:** [May 23, 2025, 6:36pm UTC](https://community.auth0.com/t/default-role-native-apps/187124/5 "2025-05-23T18:36:45Z")

</div>

Awesome, thank you so much.

---

<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:** [June 6, 2025, 6:36pm UTC](https://community.auth0.com/t/default-role-native-apps/187124/6 "2025-06-06T18:36:59Z")

</div>

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