# Add new user to role

**URL:** <https://community.auth0.com/t/add-new-user-to-role/36464>\
**Category:** Get Help\
**Tags:** hooks, auth0\
**Created:** [January 17, 2020, 8:20am UTC](https://community.auth0.com/t/add-new-user-to-role/36464 "2020-01-17T08:20:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattias.skog](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mattias.skog/32/12776_2.png) [@mattias.skog](https://community.auth0.com/u/mattias.skog)\
**Post date:** [January 17, 2020, 8:20am UTC](https://community.auth0.com/t/add-new-user-to-role/36464/1 "2020-01-17T08:20:20Z")

</div>

Hi,

I would like to add all new users created/registered in a specific connection (database) to a specific role the was created in Auth0 Dashboard (Authorisation Core). What would be the best way to achieve this?

To me, the most natural place to do this would be using a Post User Registration Hook. Can it be done? How, in that case? Any other ideas?

I was also thinking about using rules but it doesn’t seem like the correct place as then I would first need to check if the user is in the role and if not, add it. Feels like this is unnecessary to do on every login as it might slow things down…

Thank you in advance!

-Mattias

---

<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:** [January 17, 2020, 7:49pm UTC](https://community.auth0.com/t/add-new-user-to-role/36464/3 "2020-01-17T19:49:41Z")

</div>

Hi @mattias.skog,

Welcome to the Auth0 Community!

Can you take a look at this FAQ and see if it solves the problem?

> [@How do I add a default role to a new user on first login?](http://community.auth0.com/t/how-do-i-add-a-default-role-to-a-new-user-on-first-login/25857):
>
> Question: How do I assign a default role to a new user on first login? Answer: If you are using the Authorization Core, you can leverage the Management API in a rule to assign a role based on login count. Example Rule: function (user, context, callback) { const count = context.stats && context.stats.loginsCount ? context.stats.loginsCount : 0; if (count \> 1) { return callback(null, user, context); } const ManagementClient = require('auth0@2.27.0').ManagementClient; …

Thanks,  
Dan

---

<div class="post-metadata">

**Author:** ![mattias.skog](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mattias.skog/32/12776_2.png) [@mattias.skog](https://community.auth0.com/u/mattias.skog)\
**Post date:** [January 20, 2020, 8:30am UTC](https://community.auth0.com/t/add-new-user-to-role/36464/4 "2020-01-20T08:30:01Z")

</div>

Hi Dan,

Thank you for the link! I tested that bur for some reason it didn’t work, will have to look into it more today. However, still feels like this is not the correct way to do it because this requires the user to log in ones before he is put into the role.

What if I would like to search for all users in a specific role from an external application using the Management API? I can’t find that specific user until he has logged in ones…or, can I perform a search based on a given connection? Don’t think it’s possible through the API, or is it?

-Mattias

---

<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:** [January 21, 2020, 7:38pm UTC](https://community.auth0.com/t/add-new-user-to-role/36464/5 "2020-01-21T19:38:36Z")

</div>

Ah yes, because you are trying to add the roles on _creation_, which may not include a login. I overlooked that, apologies.

It sounds like for your use case the post user reg hook will be the best place to do this, like you initially mentioned.

You should register the hook as a M2M app, give the app management API permissions, then you can make the call directly from the hook. It will be similar to the rule I linked.

Does that make sense?

Let me know if you need help on specific parts of it.

Thanks,  
Dan

---

<div class="post-metadata">

**Author:** ![mattias.skog](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mattias.skog/32/12776_2.png) [@mattias.skog](https://community.auth0.com/u/mattias.skog)\
**Post date:** [January 22, 2020, 1:07pm UTC](https://community.auth0.com/t/add-new-user-to-role/36464/6 "2020-01-22T13:07:23Z")

</div>

Thanx Dan for the response! It makes sense…some code example for the hook would be welcome.

---

<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:** [January 22, 2020, 5:53pm UTC](https://community.auth0.com/t/add-new-user-to-role/36464/7 "2020-01-22T17:53:17Z")

</div>

@mattias.skog

This should work for you.

```auto
module.exports = function (user, context, cb) {
  console.log('Starting add-role-to-new-user...');
  
  var ManagementClient = require('auth0@2.19.0').ManagementClient;
  var management = new ManagementClient({
    domain: '{DOMAIN}',
    clientId: '{CLIENT ID}',
    clientSecret: '{CLIENT SECRET}',
    scope: 'update:users'
  });
  
  var params = {id : 'auth0|' + user.id};
  var data = {"roles" :["{ROLE ID}"]};
  
  management.assignRolestoUser(params, data, function (err) {
    if (err) {
      console.log(err);
    }
    console.log('Role added to new user.');
  });

  cb();
};

```

You will need to register a new M2M app as your hook, and give it `update:users` permissions in the management api. Then find your role id using the management api explorer get roles endpoint. Also be conscious of management api rate limits, you may want to build in some error handling if you expect a high volume. Look here for guidance:

> **[Auth0 Hooks](https://auth0.com/docs/customize/hooks)**
>
> Describes Auth0 hooks, some of which may be used with database and passwordless connections.

---

<div class="post-metadata">

**Author:** ![mattias.skog](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mattias.skog/32/12776_2.png) [@mattias.skog](https://community.auth0.com/u/mattias.skog)\
**Post date:** [January 24, 2020, 5:22am UTC](https://community.auth0.com/t/add-new-user-to-role/36464/8 "2020-01-24T05:22:08Z")

</div>

Thank you! This was exactly what I was looking for…

---

<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:** [January 24, 2020, 8:34pm UTC](https://community.auth0.com/t/add-new-user-to-role/36464/9 "2020-01-24T20:34:35Z")

</div>

Great! Glad it worked for you.

---

<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:** [February 8, 2020, 8:34pm UTC](https://community.auth0.com/t/add-new-user-to-role/36464/10 "2020-02-08T20:34:35Z")

</div>

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