# Adding custom claims in post-login Action when refreshing token

**URL:** <https://community.auth0.com/t/adding-custom-claims-in-post-login-action-when-refreshing-token/128708>\
**Category:** Get Help\
**Tags:** extensibility, actions-extensibility\
**Created:** [March 1, 2024, 12:55pm UTC](https://community.auth0.com/t/adding-custom-claims-in-post-login-action-when-refreshing-token/128708 "2024-03-01T12:55:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwoodbri](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mwoodbri/32/60769_2.png) [@mwoodbri](https://community.auth0.com/u/mwoodbri)\
**Post date:** [March 1, 2024, 12:55pm UTC](https://community.auth0.com/t/adding-custom-claims-in-post-login-action-when-refreshing-token/128708/1 "2024-03-01T12:55:03Z")

</div>

We have a post-login action to add custom claims to access tokens depending on the requested scopes - this works fine. We’re using it with auth0-angular, which automatically attempts refreshes the access token when necessary. This triggers our action, as expected. But as noted elsewhere ([1](https://community.auth0.com/t/original-granted-scopes-not-not-available-in-rules-action-when-refreshing-token/72419/4), [2](https://github.com/auth0/auth0-spa-js/issues/1083)) the originally requested scopes are not available at this point (contrary to [3](https://community.auth0.com/t/knowledge-find-requested-scopes-in-actions-for-refresh-token-client-credential-exchange-or-ropg/126154)). So, unlike when logging in, we don’t know which claims to add when the token is refreshed.

Does anyone have an effective workaround for this? The auth0-angular FAQ suggests that it’s possible to manually refresh tokens. I guess we could do this and pass the scopes from the existing access token, but it isn’t ideal. Any ideas much 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:** [March 12, 2024, 10:29pm UTC](https://community.auth0.com/t/adding-custom-claims-in-post-login-action-when-refreshing-token/128708/3 "2024-03-12T22:29:28Z")

</div>

Hey there @mwoodbri welcome to the community!

> [@mwoodbri](#):
>
> We have a post-login action to add custom claims to access tokens depending on the requested scopes - this works fine.

How are you checking for requested scopes initially? As far as I can tell in some quick testing `event.transaction.requested_scopes` returns the same scopes in my initial login and successful silent auth (refresh token exchange).

---

<div class="post-metadata">

**Author:** ![mwoodbri](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mwoodbri/32/60769_2.png) [@mwoodbri](https://community.auth0.com/u/mwoodbri)\
**Post date:** [March 13, 2024, 10:52am UTC](https://community.auth0.com/t/adding-custom-claims-in-post-login-action-when-refreshing-token/128708/4 "2024-03-13T10:52:51Z")

</div>

Many thanks for the pointer. We’re currently checking `scope` and `audience` from `event.request.query`. I’ll have a look at getting scopes from `event.transaction.requested_scopes` and report back. Maybe we should also be using `event.resource_server.identifier` for audience.

---

<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 13, 2024, 10:38pm UTC](https://community.auth0.com/t/adding-custom-claims-in-post-login-action-when-refreshing-token/128708/5 "2024-03-13T22:38:27Z")

</div>

No problem, happy to help where I can! Keep us posted 🙂

---

<div class="post-metadata">

**Author:** ![mwoodbri](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mwoodbri/32/60769_2.png) [@mwoodbri](https://community.auth0.com/u/mwoodbri)\
**Post date:** [March 15, 2024, 11:54am UTC](https://community.auth0.com/t/adding-custom-claims-in-post-login-action-when-refreshing-token/128708/6 "2024-03-15T11:54:00Z")

</div>

Hi, thanks again for the suggestion. In our post-login Action we replaced `event.request.query.scope` with `event.transaction.requested_scopes`. Unfortunately this hasn’t resolved the issue: it works fine for initial login but the value is still undefined for refresh token exchange. It sounds like it worked for you so perhaps this is a configuration issue - if you have any further suggestions for debugging then they would be gratefully received.

---

<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 15, 2024, 9:28pm UTC](https://community.auth0.com/t/adding-custom-claims-in-post-login-action-when-refreshing-token/128708/7 "2024-03-15T21:28:49Z")

</div>

Hey there @mwoodbri thanks for following up - My bad, I just tested again (and did some more research internally) and found that `requested_scopes` will return `undefined` which is expected behavior - Long story short, this is expected behavior because there technically are no requested scopes on the refresh token exchange. That being said, we are working to expose a `event.refreshToken` object which will contain this info. I am unfortunately unable to provide an ETA at this moment.

A potential workaround is to call [this endpoint](https://auth0.com/docs/api/management/v2/refresh-tokens/get-refresh-token) of the Management API, which _will_ return the refresh tokens scope and be the same information contained in the eventual `event.refreshToken` object. To call this endpoint, you will need to configure your action to use the node management client. The following should be helpful:

[https://community.auth0.com/t/how-can-i-use-the-management-api-in-actions/64947](https://community.auth0.com/t/how-can-i-use-the-management-api-in-actions/64947)

---

<div class="post-metadata">

**Author:** ![mwoodbri](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mwoodbri/32/60769_2.png) [@mwoodbri](https://community.auth0.com/u/mwoodbri)\
**Post date:** [March 18, 2024, 2:30pm UTC](https://community.auth0.com/t/adding-custom-claims-in-post-login-action-when-refreshing-token/128708/8 "2024-03-18T14:30:10Z")

</div>

Thanks - that sounds really promising. Do you know which plan is necessary to use the `refresh-tokens` endpoint? We have Developer but are seeing the following:

```auto
{
  "statusCode": 403,
  "error": "Forbidden",
  "message": "Subscription missing entitlement",
  "errorCode": "feature_not_enabled"
}

```

---

<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 18, 2024, 11:54pm UTC](https://community.auth0.com/t/adding-custom-claims-in-post-login-action-when-refreshing-token/128708/9 "2024-03-18T23:54:05Z")

</div>

Ah, I just realized this endpoint is [currently in EA](https://auth0.com/docs/secure/tokens/manage-refresh-tokens-with-auth0-management-api) for Enterprise plans - The goal is for it to be GA by the end of this month 🤞

---

<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:** [April 1, 2024, 11:55pm UTC](https://community.auth0.com/t/adding-custom-claims-in-post-login-action-when-refreshing-token/128708/10 "2024-04-01T23:55:04Z")

</div>

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