# Role set on first login, but deleted on subsequent login

**URL:** <https://community.auth0.com/t/role-set-on-first-login-but-deleted-on-subsequent-login/89323>\
**Category:** Get Help\
**Tags:** auth0, roles\
**Created:** [August 23, 2022, 11:32pm UTC](https://community.auth0.com/t/role-set-on-first-login-but-deleted-on-subsequent-login/89323 "2022-08-23T23:32:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![colinriddell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/colinriddell/32/33715_2.png) [@colinriddell](https://community.auth0.com/u/colinriddell)\
**Post date:** [August 23, 2022, 11:32pm UTC](https://community.auth0.com/t/role-set-on-first-login-but-deleted-on-subsequent-login/89323/1 "2022-08-23T23:32:51Z")

</div>

Hello good people of Auth0.I can’t help but feel this should be easier than it is… so maybe you can help unfuddle me. I’m setting a role on first login, based off of a query param using `context.request.query.role` like so with the following rule. But it’s un-setting the role in the metadata on subsequent logins. (Role itself is never set, so seems like I’m faking roles with this metadata array that everyone else seems to be doing too)

```auto
function (user, context, callback) {
		const NAMESPACE = "http://mynamespace.com";
    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;
    const management = new ManagementClient({
      token: auth0.accessToken,
      domain: auth0.domain
    });
    let roles = [context.request.query.role];
    const params = { id : user.user_id};
    const data = { "roles" : roles};
  
  
   // resorting to assigning everything -
    context.idToken[`${NAMESPACE}/roles`] = roles;
    context.accessToken[`${NAMESPACE}/roles`] = roles;
    context.authorization.roles = roles;

    management.users.assignRoles(params, data, function (err, user) {
    if (err) {
        // Handle error.
        console.log(err);
     }
    callback(null, user, context);
    });
    
}

```

I would like that role to be set against that user… and it does appear in the session for that user on the first login.  
When logging in again on route that doesn’t provide the query param, the roles array then disappears. I thought this rule would only set it on the **first login** , then it would persist?

How do I make it so a role is assigned on first login, and it stays assigned?

All help appreciated.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [August 25, 2022, 1:24am UTC](https://community.auth0.com/t/role-set-on-first-login-but-deleted-on-subsequent-login/89323/3 "2022-08-25T01:24:52Z")

</div>

Hey there @colinriddell welcome to the community!

> [@colinriddell](#):
>
> I would like that role to be set against that user… and it does appear in the session for that user on the first login.

Can you clarify what you mean by the fact that it does appear in the session for the user?

I’m assuming that when you say the role isn’t set, following login with the query param, you don’t get the role with [this endpoint](https://auth0.com/docs/api/management/v2/#!/Users/get_user_roles)?

Let us know!

---

<div class="post-metadata">

**Author:** ![colinriddell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/colinriddell/32/33715_2.png) [@colinriddell](https://community.auth0.com/u/colinriddell)\
**Post date:** [August 29, 2022, 7:09pm UTC](https://community.auth0.com/t/role-set-on-first-login-but-deleted-on-subsequent-login/89323/4 "2022-08-29T19:09:11Z")

</div>

Hey @ty.frith - thanks very much for your response.

So yeah. When logging in and setting the role with the query param as above, I want the role to remain in the `app_metadata`, no matter what happens.  
Though for some reason . . (and presumably down to my logic in the rule ?) when logging in _without_ the query then the metadata disappears. Does that make sense @ty.frith ? Not sure what I’m doing wrong. All help welcome.

---

<div class="post-metadata">

**Author:** ![colinriddell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/colinriddell/32/33715_2.png) [@colinriddell](https://community.auth0.com/u/colinriddell)\
**Post date:** [August 29, 2022, 9:15pm UTC](https://community.auth0.com/t/role-set-on-first-login-but-deleted-on-subsequent-login/89323/5 "2022-08-29T21:15:17Z")

</div>

Can I ask this question differently…

How do I assign a role on signup/register, and have it stick with that user?

Thanks

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [August 29, 2022, 10:55pm UTC](https://community.auth0.com/t/role-set-on-first-login-but-deleted-on-subsequent-login/89323/7 "2022-08-29T22:55:01Z")

</div>

Hey @colinriddell no problem, happy to help!

It depends on whether you want to assign roles in [app\_metadata](https://auth0.com/docs/manage-users/user-accounts/metadata) OR in [authorization core](https://auth0.com/docs/manage-users/access-control/rbac). The latter is a bit more complicated/expensive in that it requires use of the Management API directly.

If you just want to add roles to `app_metadata`, which is a totally valid approach, you might want to switch to [Actions](https://auth0.com/docs/customize/actions) and add the roles in to `app_metadata` in a Post Login Action similar to:

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

let roles = event.request.roles;
 
 //add roles to app_metadata - Leaving out any sort of counting logic
  api.user.setAppMetadata('roles', roles);

};

```

And then add them to a token(s) in another Post Login Action similar to:

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

const namespace = 'https://myapp.example.com'

  //add app_metadata roles to token(s)
  api.idToken.setCustomClaim(`${namespace}/app_metadata_roles`, event.user.app_metadata.roles);
  api.accessToken.setCustomClaim(`${namespace}/app_metadata_roles`, event.user.app_metadata.roles);
  
};

```

You’ll just want to make sure you [attach these to a flow](https://auth0.com/docs/customize/actions/write-your-first-action#attach-the-action-to-a-flow) in the right order. Please also note that I haven’t tested this personally, so you’ll want to be sure and test in your environment 🤠

Hope this helps!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [August 30, 2022, 3:41pm UTC](https://community.auth0.com/t/role-set-on-first-login-but-deleted-on-subsequent-login/89323/8 "2022-08-30T15:41:03Z")

</div>

This lab on enriching user profiles may be useful as well:

[https://auth0.com/developers/hub/labs/actions/enrich-user-profiles](https://auth0.com/developers/hub/labs/actions/enrich-user-profiles)

---

<div class="post-metadata">

**Author:** ![colinriddell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/colinriddell/32/33715_2.png) [@colinriddell](https://community.auth0.com/u/colinriddell)\
**Post date:** [August 30, 2022, 4:40pm UTC](https://community.auth0.com/t/role-set-on-first-login-but-deleted-on-subsequent-login/89323/9 "2022-08-30T16:40:13Z")

</div>

Hey @ty.frith - thanks very much for this.

In the time between posts I managed to get what I wanted to work. Here’s my update rule:

```auto
function (user, context, callback) {
    const NAMESPACE = "http://mydomain.com";
    const count = context.stats && context.stats.loginsCount ? context.stats.loginsCount : 0;
  
    if (count > 1) {
        // for every login after the first, set the roles into the token from the previously set metadata
        context.idToken[`${NAMESPACE}/roles`] = user.roles;
        context.accessToken[`${NAMESPACE}/roles`] = user.roles;
        context.authorization.roles = user.roles;
        return callback(null, user, context);
    }
    // everything onwards is first login only:

    const ManagementClient = require('auth0@2.27.0').ManagementClient;
    const management = new ManagementClient({
      token: auth0.accessToken,
      domain: auth0.domain
    });
    let roles = [context.request.query.role];
    const params = { id : user.user_id};
    const data = { "roles" : roles};
  
     // for the first login set the roles from the assigned login role (from the query string)
    context.idToken[`${NAMESPACE}/roles`] = roles;
    context.accessToken[`${NAMESPACE}/roles`] = roles;
    context.authorization.roles = roles;

    // is this even needed now? I'm not sure it's even working!
    management.users.assignRoles(params, data, function (err, user) {
    if (err) {
        // Handle error.
        console.log(err);
     }
    callback(null, user, context);
    });
  
  
  user.app_metadata = user.app_metadata || {};
  // update the app_metadata that will be part of the response
  user.app_metadata.roles = user.app_metadata.roles || [];
  user.app_metadata.roles.push(roles[0]);

  // persist the app_metadata update
  auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
    .then(function(){
      callback(null, user, context);
    })
    .catch(function(err){
      callback(err);
    });
    
}

```

So I managed to set the roles into metadata for the first login. And then for subsiquent logins read the roles from that metadata and populate it into the session through the token. Does that look legit to you @ty.frith ?

Thanks again.  
Colin

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [August 31, 2022, 1:23am UTC](https://community.auth0.com/t/role-set-on-first-login-but-deleted-on-subsequent-login/89323/10 "2022-08-31T01:23:28Z")

</div>

Hey @colinriddell thanks for following up and good to know!

> [@colinriddell](#):
>
> So I managed to set the roles into metadata for the first login. And then for subsiquent logins read the roles from that metadata and populate it into the session through the token. Does that look legit to you @ty.frith ?

Your approach looks good to me! If you’re just using the `app_metadata` then you can skip the `ManagementClient` bit 🙂 You would be setting those roles directly via the Management API as I mentioned previously.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [September 15, 2022, 1:23am UTC](https://community.auth0.com/t/role-set-on-first-login-but-deleted-on-subsequent-login/89323/11 "2022-09-15T01:23:28Z")

</div>

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