Problem updating app metadata in onExecutePreUserRegistration action

I am having a problem converting a pre-registration hook to a pre user registration action.

The problem is I cannot seem to update the user.app_metadata. Refer to the code below. What is happening before this is we are calling our system to create the user and get a user id and some account information. That is all working wonderfully. To debug I show the user before and after I attempt to update

console.log(‘before user update’);
console.log( event.user);
//this data below looks all good coming back from our system
console.log( resp.body.app_metadata.a_id);
console.log( resp.body.app_metadata.u_id);
console.log( resp.body.app_metadata.role);
//I tried this first and it did update the user in the console.log but not when looking at user after
//event.user.app_metadata = resp.body.app_metadata;

//so I then tried this:
api.user.setAppMetadata(“a_id”, resp.body.app_metadata.a_id);
api.user.setAppMetadata(“u_id”, resp.body.app_metadata.u_id);
api.user.setAppMetadata(“role”, resp.body.app_metadata.role);

console.log(‘after user update’);

//here app_metadata still shows as an empty JSON object
console.log(event.user);

//the user when inquiring after the fact also shows no app_metadata

Hi @scott8,

Thanks for reaching out to the Auth0 Community!

I understand that you’re trying to migrate from Auth0 Hooks to Actions and have issues updating the user’s app_metadata.

After testing your code snippet, I can confirm that it works and the user’s app_metadata is being updated with the correct information. You can verify by checking the user’s profile either on the Auth0 Dashboard or with the Management API v2 Get a user endpoint.

Now, if you plan on using the new metadata for additional logic, you’ll need to use event.user.app_metadata = resp.body.app_metadata; as well to be able to see the current user’s app_metadata.

Hoped this helps!

Please let me know if there’s anything else I can do to help.

Thank you.

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