# Assigning User roles on Custom Database action script

**URL:** <https://community.auth0.com/t/assigning-user-roles-on-custom-database-action-script/68184>\
**Category:** Get Help\
**Tags:** custom-database, roles\
**Created:** [August 19, 2021, 1:31am UTC](https://community.auth0.com/t/assigning-user-roles-on-custom-database-action-script/68184 "2021-08-19T01:31:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dev-support](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dev-support/32/12814_2.png) [@dev-support](https://community.auth0.com/u/dev-support)\
**Post date:** [August 19, 2021, 1:31am UTC](https://community.auth0.com/t/assigning-user-roles-on-custom-database-action-script/68184/1 "2021-08-19T01:31:11Z")

</div>

How can i assign a role when creating a user through custom database action acript ?  
When users try to login first I’m checking if they are already in the auth0 database and if not, I’m authenticating them against a 3rd party idp(AWS cognito), and upon successful authentication i then transfer this user to auth0 for future login requests. this works well, I can assign user\_metadata and app\_metadata by adding that to the profile on migrations. But I want to assign an existing custom role in the auth0 when we create this user. How can I do that, I tried passing a “roles” property to normalize profile but it didn’t work. any ideas ?

---

<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:** [August 19, 2021, 8:53pm UTC](https://community.auth0.com/t/assigning-user-roles-on-custom-database-action-script/68184/3 "2021-08-19T20:53:35Z")

</div>

Hi @dev-support,

Welcome to the Auth0 Community!

Have you tried adding it via the management api? Specifically [this endpoint](https://auth0.com/docs/api/management/v2#!/Roles/post_role_users).

Here is an example rule that might be insightful:

> [@How do I add a default role to a new user on first login?](https://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; …

---

<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:** [October 13, 2021, 6:39pm UTC](https://community.auth0.com/t/assigning-user-roles-on-custom-database-action-script/68184/4 "2021-10-13T18:39:28Z")

</div>

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