# Add roles to user in Custom database migration

**URL:** <https://community.auth0.com/t/add-roles-to-user-in-custom-database-migration/35885>\
**Category:** Get Help\
**Tags:** migration, roles\
**Created:** [January 6, 2020, 7:44pm UTC](https://community.auth0.com/t/add-roles-to-user-in-custom-database-migration/35885 "2020-01-06T19:44:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luis.borge](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/luis.borge/32/13158_2.png) [@luis.borge](https://community.auth0.com/u/luis.borge)\
**Post date:** [January 6, 2020, 7:44pm UTC](https://community.auth0.com/t/add-roles-to-user-in-custom-database-migration/35885/1 "2020-01-06T19:44:41Z")

</div>

Hi,

I implemented a Login script that retrieves from a legacy database all the information related to users (name, email…), including Roles.  
I would like to assign those Roles to the user in the moment it is migrated, so when the Token is generated it will include the permissions for the predefined Role.

- Can I assign the Roles on the Login Script? If so, can you provide an example.
- If it is not possible. What other option do I have to accomplish this?

Thanks,

---

<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 8, 2020, 7:44pm UTC](https://community.auth0.com/t/add-roles-to-user-in-custom-database-migration/35885/3 "2020-01-08T19:44:29Z")

</div>

Hi @luis.borge,

Are you migrating that database to Auth0 and you want to migrate the roles? Or are you wanting to just add the role to the user profile during the login script?

If you are doing the latter, you can use app\_metadata. Mentioned [here](https://auth0.com/docs/connections/database/custom-db/templates/login#callback-profile-parameter-example).

Hope this helps!

Thanks,  
Dan

---

<div class="post-metadata">

**Author:** ![James.Morrison](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/james.morrison/32/13507_2.png) [@James.Morrison](https://community.auth0.com/u/James.Morrison)\
**Post date:** [January 10, 2020, 8:36pm UTC](https://community.auth0.com/t/add-roles-to-user-in-custom-database-migration/35885/4 "2020-01-10T20:36:43Z")

</div>

Customer responded via email:

> Hi,
> 
> I am trying to do the first and migrate the roles to the Roles and Permissions of Auth0.
> 
> Thanks,

---

<div class="post-metadata">

**Author:** ![luis.borge](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/luis.borge/32/13158_2.png) [@luis.borge](https://community.auth0.com/u/luis.borge)\
**Post date:** [January 10, 2020, 9:00pm UTC](https://community.auth0.com/t/add-roles-to-user-in-custom-database-migration/35885/5 "2020-01-10T21:00:08Z")

</div>

I managed to add the Role to the migrated user using a Rule.  
On the Login script I add the roles to the app\_metada of the user and then I retrieved them in the Rule to add them as Auth0 Roles.  
However in the first login the permissions are not loaded in the token. Any ideas?  
Here is the code of my rule

> function (user, context, callback) {
> 
> // short-circuit if the user signed up already or is using a refresh token  
> if (context.stats.loginsCount \> 1 || context.protocol === ‘oauth2-refresh-token’) {  
> return callback(null, user, context);  
> }
> 
> const rolesIds = ;
> 
> // Role management was introduced in 2.17.0.  
> // 2.17.1 intermittently returned package not found error.  
> // Unspecified version didn’t support roles, so must be earlier than 2.17.0  
> var ManagementClient = require(‘auth0@2.17.0’).ManagementClient;  
> var management = new ManagementClient({  
> token: auth0.token,  
> domain: auth0.domain  
> });
> 
> var params = {  
> per\_page: 10,  
> page: 0  
> };
> 
> management.getRoles(params, function (err, roles) {
> 
> ```
> for (var i = 0; i < roles.length; i++) {
> if (user.app_metadata.roles.includes(roles[i].name)){
> rolesIds.push(roles[i].id);
> }
> }
> 
> // Update the user's roles
> management.assignRolestoUser({ id : user.user_id }, { roles: rolesIds }, (err) => {
> 
> if (!err) {
> console.log('Roles [' + rolesIds.join(', ') + '] assigned to user [' + user.email + ']');
> } else {
> console.error(err);
> }
> 
> return callback(err, user, context);
> 
> });
> 
> ```
> 
> });  
> }

---

<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 25, 2020, 1:00am UTC](https://community.auth0.com/t/add-roles-to-user-in-custom-database-migration/35885/6 "2020-01-25T01:00:09Z")

</div>

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