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 ?
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").
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 ');
}
}
});
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