Rule to Action migration: User AppMetadata update

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.

1 Like

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 knowledge solution.

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

(Reference: Actions Triggers: post-login - Event Object)

I also suggest testing your Actions using the built-in debugger interface to ensure your script runs correctly before deploying it to your login flow.

Thanks,
Rueben

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.

1 Like

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)
1 Like

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 });

1 Like

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

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