# Assigning user roles in a rule

**URL:** <https://community.auth0.com/t/assigning-user-roles-in-a-rule/38461>\
**Category:** Get Help\
**Tags:** rules, management-api, auth0\
**Created:** [February 25, 2020, 7:31pm UTC](https://community.auth0.com/t/assigning-user-roles-in-a-rule/38461 "2020-02-25T19:31:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jorge](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jorge/32/19135_2.png) [@jorge](https://community.auth0.com/u/jorge)\
**Post date:** [February 25, 2020, 7:31pm UTC](https://community.auth0.com/t/assigning-user-roles-in-a-rule/38461/1 "2020-02-25T19:31:57Z")

</div>

I have auth0 connected to a database in mongodb, I already did the login process from auth0 to my database, but I have a problem, the user created it in my database and until he logs in it doesn’t matter to auth0, so I can’t assign him the role until he exists in auth0, my idea is to create a rule that when the user logs in for the first time that auth0 matters it will fire a rule that assigns him the role, which comes in the data of mongo db, I appreciate your help

```
function (user, context, callback) {
    	const axios = require('axios');
      function getAccessToken() {
        return axios.post(
          "https://" + auth0.domain + "/oauth/token",
          {
            grant_type: "client_credentials",
            client_id: configuration.MGMT_API_ID,
            client_secret: configuration.MGMT_API_SECRET,
            audience: "https://" + auth0.domain + "/api/v2/"
          },
          { headers: { "content-type": "application/json" } }
        );
      }
      
      function assignRole(userId, roleId, accessToken) {
        return axios.post(
          `https://${auth0.domain}/api/v2/users/${userId}/roles`,
          { roles: [roleId] },
          {
            headers: {
              "cache-control": "no-cache",
              authorization: `Bearer ${accessToken}`,
              "content-type": "application/json"
            }
          }
        );
      }

    }

```

---

<div class="post-metadata">

**Author:** ![rashid779939](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rashid779939/32/27027_2.png) [@rashid779939](https://community.auth0.com/u/rashid779939)\
**Post date:** [February 26, 2020, 6:05am UTC](https://community.auth0.com/t/assigning-user-roles-in-a-rule/38461/2 "2020-02-26T06:05:21Z")

</div>

I am not sure i am getting your question correct. Are you migrationg user to auth0 when user login first time? If it is the case you can use post-registration hook and assign role to user when user has been created successfully at auth0.

---

<div class="post-metadata">

**Author:** ![mathiasconradt](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mathiasconradt/32/12758_2.png) [@mathiasconradt](https://community.auth0.com/u/mathiasconradt)\
**Post date:** [February 26, 2020, 8:29am UTC](https://community.auth0.com/t/assigning-user-roles-in-a-rule/38461/3 "2020-02-26T08:29:53Z")

</div>

Sounds similar to

> [@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; …

> [@Assign Roles to users using Rules](http://community.auth0.com/t/assign-roles-to-users-using-rules/27783):
>
> Hi All, At the moment, we are using “Attach Role” Management API to assign a role to users. Is there a way to do this via Rules? Currently when using rules… it is creating roles in app\_metadata or user\_metadata. Thanks, Bharathi

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [February 26, 2020, 4:43pm UTC](https://community.auth0.com/t/assigning-user-roles-in-a-rule/38461/4 "2020-02-26T16:43:20Z")

</div>

Let us know if you find the solution to your struggles in those threads!

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [January 8, 2024, 8:05am UTC](https://community.auth0.com/t/assigning-user-roles-in-a-rule/38461/5 "2024-01-08T08:05:24Z")

</div>

Hey there!

As this topic is related to Rules - Hooks - Actions, I’m excited to let you know about our next [Ask me Anything session in the Forum on Thursday, January 18 with the Rules, Hooks and Actions team](https://community.auth0.com/t/auth0-community-ask-me-anything-rules-hooks-and-why-actions-matter/124043) on Rules & Hooks and why Actions matter! Submit your questions in the thread above and our esteemed product experts will provide written answers on January 18. Find out more about Rules & Hooks and why Actions matter! Can’t wait to see you there!

[Learn more here!](https://community.auth0.com/t/auth0-community-ask-me-anything-rules-hooks-and-why-actions-matter/124043)

---

<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:** [January 22, 2024, 8:05am UTC](https://community.auth0.com/t/assigning-user-roles-in-a-rule/38461/6 "2024-01-22T08:05:44Z")

</div>

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