# Post Login Action not Placing Role in Token

**URL:** <https://community.auth0.com/t/post-login-action-not-placing-role-in-token/80018>\
**Category:** Get Help\
**Created:** [March 16, 2022, 4:52pm UTC](https://community.auth0.com/t/post-login-action-not-placing-role-in-token/80018 "2022-03-16T16:52:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kcwardwell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kcwardwell/32/35704_2.png) [@kcwardwell](https://community.auth0.com/u/kcwardwell)\
**Post date:** [March 16, 2022, 4:52pm UTC](https://community.auth0.com/t/post-login-action-not-placing-role-in-token/80018/1 "2022-03-16T16:52:44Z")

</div>

I am attempting to add roles to the access token with the example code:

exports.onExecutePostLogin = async (event, api) =\> {  
const namespace = ‘[https://artapi.com](https://artapi.com)’;  
if (event.authorization) {  
api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);  
api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);  
}  
}

When I login I see no difference in the token. I logging in for a react app using the @auth0/auth0-react library.

I am new to Actions. Where am I going wrong?

---

<div class="post-metadata">

**Author:** ![vijay-veeranki](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vijay-veeranki/32/35944_2.png) [@vijay-veeranki](https://community.auth0.com/u/vijay-veeranki)\
**Post date:** [March 16, 2022, 10:04pm UTC](https://community.auth0.com/t/post-login-action-not-placing-role-in-token/80018/4 "2022-03-16T22:04:03Z")

</div>

I am facing the same issue, we want to get the token updated with user GitHub-teams, our rule works fine, converting that to action is not working, can some on help to get the token updated with git hub teams.

---

<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:** [March 16, 2022, 10:38pm UTC](https://community.auth0.com/t/post-login-action-not-placing-role-in-token/80018/5 "2022-03-16T22:38:39Z")

</div>

Hey there @kcwardwell 🙂

> [@kcwardwell](#):
>
> exports.onExecutePostLogin = async (event, api) =\> {  
> const namespace = ‘[https://artapi.com](https://artapi.com)’;  
> if (event.authorization) {  
> api.idToken.setCustomClaim( `${namespace}/roles` , event.authorization.roles);  
> api.accessToken.setCustomClaim( `${namespace}/roles` , event.authorization.roles);  
> }  
> }

Is this your action code verbatim? I did notice that your claims are missing quotes which I believe will cause issues. It should be:

```auto
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://my-app.example.com';
  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
  }
}

```

Aside from this, can you confirm that you have [attached the action](https://auth0.com/docs/customize/actions/write-your-first-action#attach-the-action-to-a-flow) to the Post Login Flow? I was just able to test this on my end and everything seems to be functioning as expected.

---

<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:** [March 16, 2022, 10:40pm UTC](https://community.auth0.com/t/post-login-action-not-placing-role-in-token/80018/6 "2022-03-16T22:40:07Z")

</div>

Hey there @vijay-veeranki welcome to the community! 🤠

> [@vijay-veeranki](#):
>
> I am facing the same issue, we want to get the token updated with user GitHub-teams, our rule works fine, converting that to action is not working, can some on help to get the token updated with git hub teams.

Are you using the same action? Do you mind sharing it here?

---

<div class="post-metadata">

**Author:** ![vijay-veeranki](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vijay-veeranki/32/35944_2.png) [@vijay-veeranki](https://community.auth0.com/u/vijay-veeranki)\
**Post date:** [March 16, 2022, 10:51pm UTC](https://community.auth0.com/t/post-login-action-not-placing-role-in-token/80018/7 "2022-03-16T22:51:33Z")

</div>

Hi,

Thanks for your reply, please find our action converted from a rule which is working fine.

Created this action and added it to the login flow.

Action:

```auto
exports.onExecutePostLogin = async (event, api) => {
  const _ = require('lodash');
  var request = require('request');

  // Apply to 'github' connections only
  if(event.connection.strategy === 'github'){
    // Get user's Github profile and API access key
    var github_identity = _.find(event.user.identities, { connection: 'github' });

    // Get list of user's Github teams
    var teams_req = {
      url: 'https://api.github.com/user/teams',
      headers: {
        'Authorization': 'token ' + github_identity.access_token,
        'User-Agent': 'request'
      }
    };

    request(teams_req, function (err, resp, body) {
      if (resp.statusCode !== 200) {
        return api.access.deny('Error retrieving teams from github: ' + body || err);
      }

      // Construct list of user's Github teams
      // The team slug is used, to normalise whitespace, capitalisation etc.
      var git_teams = JSON.parse(body).map(function (team) {
        if (team.organization.login === "orgname") {
          return "github:" + team.slug;
        }
      });

      // Add team list to the user's JWT as a custom claim
      //
      // Custom OIDC claims should be prefixed with a unique value
      // to prevent clashes with claims from other sources.
      // Common practice is to use a URL
      api.idToken.setCustomClaim[event.secrets.K8S_OIDC_GROUP_CLAIM_DOMAIN] = git_teams;
      return;
    });
  } else {
    return;
  }
};

```

the rule which is working fine, which we are using for couple of years:

```auto
function (user, context, callback) {
  var request = require('request');

  // Apply to 'github' connections only
  if(context.connection === 'github'){
    // Get user's Github profile and API access key
    var github_identity = _.find(user.identities, { connection: 'github' });

    // Get list of user's Github teams
    var teams_req = {
      url: 'https://api.github.com/user/teams',
      headers: {
        'Authorization': 'token ' + github_identity.access_token,
        'User-Agent': 'request'
      }
    };

    request(teams_req, function (err, resp, body) {
      if (resp.statusCode !== 200) {
        return callback(new Error('Error retrieving teams from github: ' + body || err));
      }

      // Construct list of user's Github teams
      // The team slug is used, to normalise whitespace, capitalisation etc.
      var git_teams = JSON.parse(body).map(function (team) {
        if (team.organization.login === "orgname") {
          return "github:" + team.slug;
        }
      });

      // Add team list to the user's JWT as a custom claim
      //
      // Custom OIDC claims should be prefixed with a unique value
      // to prevent clashes with claims from other sources.
      // Common practice is to use a URL
      context.idToken[configuration.K8S_OIDC_GROUP_CLAIM_DOMAIN] = git_teams;

      return callback(null, user, context);
    });
  } else {
    return callback(null, user, context);
  }
}

```

---

<div class="post-metadata">

**Author:** ![vijay-veeranki](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vijay-veeranki/32/35944_2.png) [@vijay-veeranki](https://community.auth0.com/u/vijay-veeranki)\
**Post date:** [March 17, 2022, 9:35pm UTC](https://community.auth0.com/t/post-login-action-not-placing-role-in-token/80018/8 "2022-03-17T21:35:08Z")

</div>

@ty.frith after some debugging noticed below

event.user.identities used in action are not getting the same result as user.identities in rules.

rules are giving

```auto
    "headers": {
      "Authorization": "token gho_xxxxxxxxxxxxxx",
      "User-Agent": "request"
    }

```

Action is giving token as “undefined”

```auto
    "headers": {
      "Authorization": "token undefined",
      "User-Agent": "request"
    },

```

---

<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:** [March 17, 2022, 11:37pm UTC](https://community.auth0.com/t/post-login-action-not-placing-role-in-token/80018/9 "2022-03-17T23:37:24Z")

</div>

Thanks for following up with your findings, very helpful!

I was unaware of the difference here, but it looks like with actions you need to poll the Management API in order to extract the user’s IDP access token as opposed to being able to extract it directly from `Identities` like was possible in rules.

I set up a basic action wherein I authenticated a user at google, and attempted to extract the access token the same way you did and got `undefined` as well. However, getting the user via the [Management API](https://auth0.com/docs/api/management/v2#!/Users/get_users_by_id) I can indeed see/extract the IDP access\_token so I believe you need to add that step in your action. Please see:

> **[Call an Identity Provider API](https://auth0.com/docs/authenticate/identity-providers/calling-an-external-idp-api)**
>
> Describes how to call an external Identity Provider API.

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:** [April 22, 2022, 7:39pm UTC](https://community.auth0.com/t/post-login-action-not-placing-role-in-token/80018/10 "2022-04-22T19:39:56Z")

</div>


