Update App Meta Data from Post User Registration

Yes, here is my hook:

exports.onExecutePostUserRegistration = async (event) => {
  const ManagementClient = require('auth0').ManagementClient;

  var management = new ManagementClient({
      domain: event.secrets.domain,
      clientId: event.secrets.clientId,
      clientSecret: event.secrets.clientSecret,
  });

  await management.updateAppMetadata({id: event.user.user_id }, { alternateId: 123});
};

auth0@2.34.2

You should only need update:users_app_metadata for this, but my M2M app also has create:users_app_metadata, read:users_app_metadata, update:users, read:users, and read:roles.

If you want to be certain that the user has been created in Auth0 before syncing with your local database, I’d recommend using a post-login action. The action could check if it is the first login with event.stats.logins_count === 1.

The intent of post-user registration actions is more geared toward alerting other systems of the signup than altering data within Auth0. There is an uncommon, yet possible chance of a race condition where the user won’t be found in the post-user registration action when attempting to update the user’s metadata. More commonly, any alterations in the user’s data are not guaranteed to be present in other types of actions and this could lead to unexpected behavior later on.

1 Like