# How to assign "admin" role, when new user signs up?

**URL:** <https://community.auth0.com/t/how-to-assign-admin-role-when-new-user-signs-up/100242>\
**Category:** Get Help\
**Tags:** user-profile, user-management\
**Created:** [February 11, 2023, 7:47am UTC](https://community.auth0.com/t/how-to-assign-admin-role-when-new-user-signs-up/100242 "2023-02-11T07:47:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chetanbc](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@chetanbc](https://community.auth0.com/u/chetanbc)\
**Post date:** [February 11, 2023, 7:47am UTC](https://community.auth0.com/t/how-to-assign-admin-role-when-new-user-signs-up/100242/1 "2023-02-11T07:47:16Z")

</div>

Hello!

I want to assign “admin” role the new user who signup through auth0 dashboard. All users who will be created by admin, will be assigned roles by admin to them. So when the users created by admin, login through dashboard, they should still have the roles assigned by admin and not the role “admin” itself.  
So in short, only the users who signup through dashboard should have “admin” role and rest of users role is decided by admin.

Please suggest how this default role assignment can be done during signup.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [February 13, 2023, 4:45pm UTC](https://community.auth0.com/t/how-to-assign-admin-role-when-new-user-signs-up/100242/3 "2023-02-13T16:45:58Z")

</div>

Hi @chetanbc,

Thanks for reaching out to the Auth0 Community!

I recommend using the Management API’s [Assign roles to a user endpoint](https://auth0.com/docs/api/management/v2#!/Users/post_user_roles) inside an Auth0 Pre-registration Action script to assign your users to the “admin” role.

Here is a sample:

```auto
exports.onExecutePostUserRegistration = async (event, api) => {

  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" : ["ROLE_ID"]};

  try {
    const res = await management.assignRolestoUser(params, data)
  } catch (e) {
    console.log(e)
    // Handle error
  } 
};

```

I recommend checking out our [How can I use the Management API in Actions?](https://community.auth0.com/t/how-can-i-use-the-management-api-in-actions/64947) FAQ for more instructions.

Please let me know if there is anything else I can do to help.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![chetanbc](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@chetanbc](https://community.auth0.com/u/chetanbc)\
**Post date:** [February 15, 2023, 2:43pm UTC](https://community.auth0.com/t/how-to-assign-admin-role-when-new-user-signs-up/100242/4 "2023-02-15T14:43:57Z")

</div>

Hi @rueben.tiow ,

Thank you for the reply. But I am getting error message about ‘user\_id’ not being present in event.user object. What am I missing here? Is it because the user\_id is not present before the registration?

Property ‘user\_id’ does not exist on type ‘{ username?: string | undefined; email?: string | undefined; app\_metadata?: AppMetadata0 | undefined; user\_metadata?: UserMetadata0 | undefined; name?: string | undefined; … 4 more …; picture?: string | undefined; }’.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [February 15, 2023, 11:30pm UTC](https://community.auth0.com/t/how-to-assign-admin-role-when-new-user-signs-up/100242/5 "2023-02-15T23:30:00Z")

</div>

Hi @chetanbc,

Thank you for your response and for testing this for me.

After checking the [Pre User Registration Action’s event object](https://auth0.com/docs/customize/actions/flows-and-triggers/pre-user-registration-flow/event-object), I can confirm that the `user_id` is unobtainable.

In this case, I made an adjustment to the code to use a [Post User Registration Action](https://auth0.com/docs/customize/actions/flows-and-triggers/post-user-registration-flow/event-object#:~:text=last%20updated/modified.-,user_id,-String.%20). This should fix the issue.

Please let me know how this goes for you.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![chetanbc](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@chetanbc](https://community.auth0.com/u/chetanbc)\
**Post date:** [February 16, 2023, 6:20pm UTC](https://community.auth0.com/t/how-to-assign-admin-role-when-new-user-signs-up/100242/6 "2023-02-16T18:20:57Z")

</div>

Hi @rueben.tiow ,

Thanks for the modifications. It does add roles to user. But it assigns role to both users registered through ‘auth0 login page’ and users created by admin. I wanted to achieve the former only.

I guess the best way for me is to create some button in frontend, say, “get paid account” and when user actually clicks it, I can internally call management api to assign the Admin role. And of-course the user can get paid account only if he pays for it.

Anyhow thanks for your inputs.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [February 16, 2023, 7:55pm UTC](https://community.auth0.com/t/how-to-assign-admin-role-when-new-user-signs-up/100242/7 "2023-02-16T19:55:18Z")

</div>

Hi @chetanbc,

Thank you for your response.

> [@chetanbc](#):
>
> But it assigns role to both users registered through ‘auth0 login page’ and users created by admin. I wanted to achieve the former only.

It may be possible to do this if the admin creates these users with some user\_metadata value, for example `"created_by_admin": true`, and using it in the Action to check if this condition is false.

```auto
if(!event.user.user_metadata.created_by_admin){
  try {
    const res = await management.assignRolestoUser(params, data)
  } catch (e) {
    console.log(e)
    // Handle error
  } 
}

```

> [@chetanbc](#):
>
> I guess the best way for me is to create some button in frontend, say, “get paid account” and when user actually clicks it, I can internally call management api to assign the Admin role. And of-course the user can get paid account only if he pays for it.

That sound’s good!

Please reach out again if you have any questions.

Thanks,  
Rueben

---

<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:** [March 2, 2023, 7:55pm UTC](https://community.auth0.com/t/how-to-assign-admin-role-when-new-user-signs-up/100242/8 "2023-03-02T19:55:35Z")

</div>

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