# Rule to Action migration: User AppMetadata update

**URL:** <https://community.auth0.com/t/rule-to-action-migration-user-appmetadata-update/147981>\
**Category:** Get Help\
**Tags:** apis, application\
**Created:** [September 25, 2024, 7:15pm UTC](https://community.auth0.com/t/rule-to-action-migration-user-appmetadata-update/147981 "2024-09-25T19:15:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![samk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/samk/32/59786_2.png) [@samk](https://community.auth0.com/u/samk)\
**Post date:** [September 25, 2024, 7:15pm UTC](https://community.auth0.com/t/rule-to-action-migration-user-appmetadata-update/147981/1 "2024-09-25T19:15:34Z")

</div>

We are in the process of migrating our rules to actions (post-login) and need quick help on this.  
we have a rule where we make a server/backend call for updating user data in our DB and the call returns some data which we need to store in Auth0 user AppMetadata.  
This below code snippet used to work fine in rule and used to update user app\_metadata  
user.app\_metadata.user\_name = userName;  
auth0.users.updateAppMetadata(user.user\_id, user.app\_metadata)  
.then(function(){  
context.idToken[“user\_name”] = userName;  
return callback(null, user, context);  
})  
As per migration doc, changed it to  
api.user.setAppMetadata(“user\_name”, userName);  
api.idToken.setCustomClaim(“user\_name”, userName);  
I don’t see a update User API call and user app\_metadata is NOT updated in the action post external call.  
What am I missing ?

Another question I have is how to check for (context.protocol === “oauth2-refresh-token”) in action ?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [September 26, 2024, 5:42pm UTC](https://community.auth0.com/t/rule-to-action-migration-user-appmetadata-update/147981/3 "2024-09-26T17:42:52Z")

</div>

Hi @samk,

Welcome to the Auth0 Community!

The code snippet you shared looks fine for setting the app\_metadata. However, I recommend using custom namespace claims to set custom claims. For an example, take a look at our [Adding custom claims to tokens](https://community.auth0.com/t/adding-custom-claims-to-tokens/84590) knowledge solution.

> [@samk](#):
>
> Another question I have is how to check for (context.protocol === “oauth2-refresh-token”) in action ?

The equivalent method in Actions would be `event.transaction.protocol === "oauth2-refresh-token")`.

(Reference: [Actions Triggers: post-login - Event Object](https://auth0.com/docs/customize/actions/explore-triggers/signup-and-login-triggers/login-trigger/post-login-event-object#:~:text=authentication%20and%20consent.-,protocol,-Optional%20string))

I also suggest [testing your Actions](https://auth0.com/docs/customize/actions/test-actions) using the built-in debugger interface to ensure your script runs correctly before deploying it to your login flow.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![samk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/samk/32/59786_2.png) [@samk](https://community.auth0.com/u/samk)\
**Post date:** [September 26, 2024, 6:54pm UTC](https://community.auth0.com/t/rule-to-action-migration-user-appmetadata-update/147981/4 "2024-09-26T18:54:48Z")

</div>

Thank you rueben. While the code snippet looks fine for setting the app\_metadata, its not updating. Log statements before and after gets executed but don’t see the updates in user AppMetadata. Same code snippet works fine before the external call but not post external call (request.post call to server) which is surprising. Its the exact same way in the rule. Trying to not increase the scope of changes in rest of the code as part of rule migration. Wonder what is different in action , post external call and if something I am missing.

Its something like this.  
request.post({  
url: externalUrl + ‘/user-reg’,  
json: data,  
timeout: 15000  
}, function(err, response, body) {  
if (err) {  
//log error… deny  
return api.access.deny(“error msg”);  
} else {  
// all good  
//below doesnt seem to work…  
var userName = body.userName;  
api.user.setAppMetadata(“user\_name”, userName);  
api.idToken.setCustomClaim(“user\_name”, userName);  
console.log('updated user metaData with userName ');  
}  
}  
});

Thanks again.

---

<div class="post-metadata">

**Author:** ![samk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/samk/32/59786_2.png) [@samk](https://community.auth0.com/u/samk)\
**Post date:** [September 26, 2024, 10:14pm UTC](https://community.auth0.com/t/rule-to-action-migration-user-appmetadata-update/147981/5 "2024-09-26T22:14:12Z")

</div>

As a workaround, tried explicitly making a API call using ManagementClient to update user AppMetadata post the external call. Something like below but ran into errors. Can you please point me to example using ManagementClient and updating user Appmetadata from action  
Tried below two ways but couldnt get them to work

1. managementClient.updateUser(event.user.user\_id, metadata);
2. managementClient.users.updateAppMetadata(params, metadata, function (err, user)

---

<div class="post-metadata">

**Author:** ![samk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/samk/32/59786_2.png) [@samk](https://community.auth0.com/u/samk)\
**Post date:** [October 2, 2024, 4:29am UTC](https://community.auth0.com/t/rule-to-action-migration-user-appmetadata-update/147981/6 "2024-10-02T04:29:01Z")

</div>

Solution that worked.  
Issue is that , I didnt have “async/await” in my external API call and its needed since all API calls in action are async.

const response = await axios.post(`${externalUrl}/user-reg` , data, { timeout: 15000 });

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [October 3, 2024, 6:00pm UTC](https://community.auth0.com/t/rule-to-action-migration-user-appmetadata-update/147981/7 "2024-10-03T18:00:04Z")

</div>

Hi @samk,

Thanks for sharing your solution with the rest of the community.

Using the ManagementClient to update the user is a totally valid approach.

Let us know if you have any further questions.

Thanks,  
Rueben

---

<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:** [October 17, 2024, 6:00pm UTC](https://community.auth0.com/t/rule-to-action-migration-user-appmetadata-update/147981/8 "2024-10-17T18:00:57Z")

</div>

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