# Set Custom Claim in callback function doesn't work

**URL:** <https://community.auth0.com/t/set-custom-claim-in-callback-function-doesnt-work/130075>\
**Category:** Get Help\
**Tags:** extensibility, actions-extensibility\
**Created:** [March 20, 2024, 10:25am UTC](https://community.auth0.com/t/set-custom-claim-in-callback-function-doesnt-work/130075 "2024-03-20T10:25:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shacharl1](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@shacharl1](https://community.auth0.com/u/shacharl1)\
**Post date:** [March 20, 2024, 10:25am UTC](https://community.auth0.com/t/set-custom-claim-in-callback-function-doesnt-work/130075/1 "2024-03-20T10:25:26Z")

</div>

I’m migrating from Rules to Actions and when and the following doesn’t work for me -

```auto
var getDb = require('mongo-getdb');
exports.onExecutePostLogin = async (event, api) => {
var namespace = 'https://mydomain.com/';
getDb('myconnectionstring', function (db) {
api.idToken.setCustomClaim(namespace + 'appId',"12345678");
}
}

```

when I put the setCustomClaim outside of the getDB callback I do get the claim in the token, but when the setCustomClaim is inside there is no claim in the token.  
any ideas?  
Thanks

---

<div class="post-metadata">

**Author:** ![marcelina.barycka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marcelina.barycka/32/56162_2.png) [@marcelina.barycka](https://community.auth0.com/u/marcelina.barycka)\
**Post date:** [March 20, 2024, 2:41pm UTC](https://community.auth0.com/t/set-custom-claim-in-callback-function-doesnt-work/130075/4 "2024-03-20T14:41:02Z")

</div>

Hey there @shacharl1 !

> [@shacharl1](#):
>
> when I put the setCustomClaim outside of the getDB callback I do get the claim in the token, but when the setCustomClaim is inside there is no claim in the token.

I think it’s a matter of function nesting and scopes (in Java Script meaning).

Any particular reason you need to do it that way?

Happy to search for the solution to your problem - just please let us know what you want to achieve with this Action script.

---

<div class="post-metadata">

**Author:** ![shacharl1](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@shacharl1](https://community.auth0.com/u/shacharl1)\
**Post date:** [March 26, 2024, 8:20am UTC](https://community.auth0.com/t/set-custom-claim-in-callback-function-doesnt-work/130075/5 "2024-03-26T08:20:30Z")

</div>

Hi,  
I need to set the custom claim based on the response I’m getting from the getDb, the response is in the callback, is that possible?

---

<div class="post-metadata">

**Author:** ![marcelina.barycka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marcelina.barycka/32/56162_2.png) [@marcelina.barycka](https://community.auth0.com/u/marcelina.barycka)\
**Post date:** [March 28, 2024, 12:52pm UTC](https://community.auth0.com/t/set-custom-claim-in-callback-function-doesnt-work/130075/6 "2024-03-28T12:52:01Z")

</div>

Hi @shacharl1 !

Actions are quite flexible but [don’t work 1:1](https://auth0.com/docs/customize/actions/migrate/migrate-from-rules-to-actions#change-the-function-declaration) as the Rules feature.

In case you would want to add custom claims if the user logs in in the context of your legacy database, or to a specific app (_login context data_) then you can use the `event` object and the properties associated: `event.connection` and `event.client`, respectively. More on that [here](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/event-object).

Later, based on that context data, you can make further login decisions / add custom claims with the `api` [object](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/api-object).

For a generic detailed design, please see [this github repo](https://github.com/Auth0-Marketplace/TEMPLATE-action-post-login). In case, more guidance is needed, [Auth0 Professional Services 39](https://auth0.com/docs/professional-services) can definitely assist in designing/implementing an architecture, aligning to security best practices.

---

<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 11, 2024, 12:52pm UTC](https://community.auth0.com/t/set-custom-claim-in-callback-function-doesnt-work/130075/7 "2024-04-11T12:52:40Z")

</div>

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