# Assign user role in login flow

**URL:** <https://community.auth0.com/t/assign-user-role-in-login-flow/185443>\
**Category:** Get Help\
**Tags:** management-api, roles\
**Created:** [April 14, 2025, 12:26pm UTC](https://community.auth0.com/t/assign-user-role-in-login-flow/185443 "2025-04-14T12:26:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpatrone1](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dpatrone1/32/48427_2.png) [@dpatrone1](https://community.auth0.com/u/dpatrone1)\
**Post date:** [April 14, 2025, 12:26pm UTC](https://community.auth0.com/t/assign-user-role-in-login-flow/185443/1 "2025-04-14T12:26:10Z")

</div>

hi everyone,  
i have a auth0 automation to add a role to a user to the first login. with the new updates the row

```auto
      await management.assignRolestoUser(params, data);

```

no logner works, i cannot find any info with my search in the docs can anyone tell me how to do it now the code some month ago was working  
the error is

> TypeError: management.assignRolestoUser is not a function

here is the complete action

```auto
exports.onExecutePostLogin = async (event, api) => {
const result = event.transaction.redirect_uri === "site link";
console.log(result + " result of redirect uri " + event.transaction.redirect_uri);

  if (event.stats.logins_count > 1) {
    return;
  }
  
  if (event.transaction.redirect_uri === "site link") {
    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" : [event.secrets.role]};

    try {
      await management.assignRolestoUser(params, data);
      console.log(`Role ${data.roles} successfully assigned to ${event.user.email}`);
      api.idToken.setCustomClaim(`rules`, "rangerGestUser");
      api.accessToken.setCustomClaim(`rules`, "rangerGestUser");
    } catch (err) {
      console.log(err);
      // Handle error.
    }
  }
};

```

---

<div class="post-metadata">

**Author:** ![nik.baleca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nik.baleca/32/68749_2.png) [@nik.baleca](https://community.auth0.com/u/nik.baleca)\
**Post date:** [April 14, 2025, 6:45pm UTC](https://community.auth0.com/t/assign-user-role-in-login-flow/185443/3 "2025-04-14T18:45:36Z")

</div>

Hi @dpatrone1

Welcome back to the Auth0 Community!

I tested the action code on my end and I was able to reproduce the error that you are receiving.

As far as I have checked in regards to the [NodeJS Management Client](https://node-auth0-docs.netlify.app/module-management.managementclient) documentation, `assignRolestoUser` is not a valid function within the available list.

You can try following the steps provided in this [blog post](https://auth0.com/blog/assign-default-role-on-sign-up-with-actions/) and use `management.users.assignRoles(param, data)` instead.

As an alternative, I would recommend to use the [Management API](https://auth0.com/docs/api/management/v2/users/post-user-roles) or to use the [updateUser](https://node-auth0-docs.netlify.app/module-management.managementclient#updateUser) function available within the Management Client.

If you have any other questions, let me know!

Kind Regards,  
Nik

---

<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:** [April 28, 2025, 6:46pm UTC](https://community.auth0.com/t/assign-user-role-in-login-flow/185443/4 "2025-04-28T18:46:10Z")

</div>

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