# Action for assigning default Role does not work

**URL:** <https://community.auth0.com/t/action-for-assigning-default-role-does-not-work/80592>\
**Category:** Get Help\
**Created:** [March 26, 2022, 11:08pm UTC](https://community.auth0.com/t/action-for-assigning-default-role-does-not-work/80592 "2022-03-26T23:08:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![lonli.lokli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lonli.lokli/32/35271_2.png) [@lonli.lokli](https://community.auth0.com/u/lonli.lokli)\
**Post date:** [March 26, 2022, 11:08pm UTC](https://community.auth0.com/t/action-for-assigning-default-role-does-not-work/80592/1 "2022-03-26T23:08:04Z")

</div>

I created new Action with 3 secrets - domain (Auth0 domain), clientId & clientSecret with this code

Unfortunately it does not add Role to the User, logs for post registration showing this  
_on post-user-registration: 400 Compilation failed: Invalid or unexpected token",_

```auto
exports.onExecutePostUserRegistration= async (event) => {
  if (event.stats.logins_count !== 1) {
    return;
  }

  const namespace = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role;

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

  const management = new ManagementClient({
    domain: event.secrets.domain,
    clientId: event.secrets.clientId,
    clientSecret: event.secrets.clientSecret,
    scope: "read:roles create:roles update:roles",
  });

  const defaultRole = { id :'rol_MYCUSTOMID'};
  
  var data = { "users" : [event.user.user_id]};

  try {
    if (event.authorization) {
      if (!event.user.email_verified) {
        return;
      } else {         
          await management.roles.assignUsers(defaultRole, data);
      }
    }
  } catch (e) {
    console.log(e);
  }

};

```

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [March 30, 2022, 2:47pm UTC](https://community.auth0.com/t/action-for-assigning-default-role-does-not-work/80592/3 "2022-03-30T14:47:37Z")

</div>

Hi @lonli.lokli,

Welcome to the Auth0 Community!

`event.stats` is not available in post registration actions. [Actions Triggers: post-user-registration - Event Object](https://auth0.com/docs/customize/actions/flows-and-triggers/post-user-registration-flow/event-object)

You probably want to do this in a post login action anyways, as post registration only runs for DB users, and is meant for async flows like adding the ID to a remote system.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![lonli.lokli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lonli.lokli/32/35271_2.png) [@lonli.lokli](https://community.auth0.com/u/lonli.lokli)\
**Post date:** [March 31, 2022, 7:00am UTC](https://community.auth0.com/t/action-for-assigning-default-role-does-not-work/80592/4 "2022-03-31T07:00:48Z")

</div>

Thanks for your comments, I already modified my code according to similar snippets in other topics.  
Also I am using Post registration as I am going to use only Passwordless clients, which are valid for this type as well.

Currently my code does not work because of Auth0 bug [Post-user-registration trigger for actions not called - #15 by tyf](https://community.auth0.com/t/post-user-registration-trigger-for-actions-not-called/70262/15)

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [March 31, 2022, 1:08pm UTC](https://community.auth0.com/t/action-for-assigning-default-role-does-not-work/80592/5 "2022-03-31T13:08:09Z")

</div>

Hmm, you shouldn’t be able to use an `event.stats.logins_count` object regardless. That should be throwing an error as `event.stats` doesn’t exist.

As I mentioned, you can resolve this issue by using a post-login action instead. It looks like the code you found was for a post-login action (hence the `event.stats.logins_count`).

The bug you are citing affects the logs and shouldn’t be throwing this error. How do you know the bug is the source of the issue?

---

<div class="post-metadata">

**Author:** ![lonli.lokli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lonli.lokli/32/35271_2.png) [@lonli.lokli](https://community.auth0.com/u/lonli.lokli)\
**Post date:** [March 31, 2022, 1:25pm UTC](https://community.auth0.com/t/action-for-assigning-default-role-does-not-work/80592/6 "2022-03-31T13:25:39Z")

</div>

My current code looks like this (clientId, ClientSecret belongs to Machine-ToMachine application with Auth0 System API access enabled)

```auto
exports.onExecutePostUserRegistration = async (event) => {
  const ManagementClient = require("auth0").ManagementClient;

  const management = new ManagementClient({
    domain: event.secrets.domain,
    clientId: event.secrets.clientId,
    clientSecret: event.secrets.clientSecret,
    scope: "update:roles create:role_members",
  });

  const defaultRole = { id: event.secrets.defaultRoleId };

  var data = { "users": [event.user.user_id] };

  try {
    await management.roles.assignUsers(defaultRole, data);
  } catch (e) {
    console.error(e);
  }

};

```

And with this code this action is not executed on PostRegistration for Passwordless user, no running events are listed in the logs

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [March 31, 2022, 3:14pm UTC](https://community.auth0.com/t/action-for-assigning-default-role-does-not-work/80592/7 "2022-03-31T15:14:11Z")

</div>

It looks like you are still using a **post registration action** here.

Try switching to a **post login action** with your conditional, as I have suggested previously.

---

<div class="post-metadata">

**Author:** ![lonli.lokli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lonli.lokli/32/35271_2.png) [@lonli.lokli](https://community.auth0.com/u/lonli.lokli)\
**Post date:** [March 31, 2022, 5:42pm UTC](https://community.auth0.com/t/action-for-assigning-default-role-does-not-work/80592/8 "2022-03-31T17:42:41Z")

</div>

Can you please explain the benefits of using your approach? Except the obvious fact that it works now, while PostRegister not due to Auth0 issue

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [March 31, 2022, 5:59pm UTC](https://community.auth0.com/t/action-for-assigning-default-role-does-not-work/80592/9 "2022-03-31T17:59:48Z")

</div>

[Post-registration actions aren’t designed to update users in Auth0](https://community.auth0.com/t/can-i-use-a-post-user-registration-action-or-hook-to-update-a-user/63423).

Have you looked at the bug report that you linked? It is just a **logging** bug, it shouldn’t have any impact on whether or not the action fires.

---

<div class="post-metadata">

**Author:** ![lonli.lokli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lonli.lokli/32/35271_2.png) [@lonli.lokli](https://community.auth0.com/u/lonli.lokli)\
**Post date:** [April 2, 2022, 4:27pm UTC](https://community.auth0.com/t/action-for-assigning-default-role-does-not-work/80592/10 "2022-04-02T16:27:17Z")

</div>

Hm you are right.  
Then it’s not clear for me why this code does not work.

Also thanks for your link, I will change code to use another qaction for that.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [April 4, 2022, 3:48pm UTC](https://community.auth0.com/t/action-for-assigning-default-role-does-not-work/80592/11 "2022-04-04T15:48:09Z")

</div>

A post was split to a new topic: [Pre registration Action does not add metadata](https://community.auth0.com/t/pre-registration-action-does-not-add-metadata/81075)

---

<div class="post-metadata">

**Author:** ![lonli.lokli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lonli.lokli/32/35271_2.png) [@lonli.lokli](https://community.auth0.com/u/lonli.lokli)\
**Post date:** [April 7, 2022, 8:10am UTC](https://community.auth0.com/t/action-for-assigning-default-role-does-not-work/80592/12 "2022-04-07T08:10:05Z")

</div>

With the great help from @dan.woda here is working solution to assign new users to specific role and also include this role

1. Create Machine-to-Machine application with client\_credentials grant and access to Auth0 System API
2. Create Post-Login action with following secrets from M2M application - DOMAIN, CLIENT\_ID, SECRET, DEFAULT\_ROLE\_ID, DFAULT\_ROLE\_NAME
3. Create action with this code and assign it to the flow. Note: I am using .NET Core app so have to put claims in specific namespace to make them available for consuming API

```auto

/**

* Handler that will be called during the execution of a PostLogin flow.

*

* @param {Event} event - Details about the user and the context in which they are logging in.

* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.

*/

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

  const namespace = 'http://schemas.microsoft.com/ws/2008/06/identity/claims/role';

  if (event.authorization && event.authorization.roles.length === 0) {

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

    const auth0 = new ManagementClient({

      domain: event.secrets.DOMAIN,

      clientId: event.secrets.CLIENT_ID,

      clientSecret: event.secrets.CLIENT_SECRET,

      scope: 'read:roles update:users create:role_members',

    })

    const params = {id: event.user.user_id}

    const data = {'roles':[event.secrets.DEFAULT_ROLE_ID]}

    await auth0.assignRolestoUser(params,data,(err) => {

      if (err) {

        console.log('DefaultRoleActionError: ', err)

      }

    })

    api.idToken.setCustomClaim(`${namespace}`, event.secrets.DEFAULT_ROLE_NAME);

    api.accessToken.setCustomClaim(`${namespace}`, event.secrets.DEFAULT_ROLE_NAME);

  } else if (event.authorization) {

    api.idToken.setCustomClaim(`${namespace}`, event.authorization.roles);

    api.accessToken.setCustomClaim(`${namespace}`, event.authorization.roles);

  }

};

```

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [April 22, 2022, 12:05am UTC](https://community.auth0.com/t/action-for-assigning-default-role-does-not-work/80592/13 "2022-04-22T00:05:19Z")

</div>


