Auth0 Home Blog Docs

Create rule to copy root meta data to user_metadata

I want to create a rule that writes certain root meta data like e.g. first_name to user_metadata.usermeta_first_name. The reason for duplicating is that we have an extension in an application that can only access section user_metadata. Can anyone help me with that? Thanks!

Hey there @christoph.moldaschl!

Can you provide more context around the usecase? What is it for? From where the first_name gonna be taken etc? Thanks!

hey @konrad.sopala, just found that we can access the root data so there is no need to duplicate the data.

The use case was to duplicate data from e.g. “given_name” to “user_metadata.given_name” . Reason: we use a CMS with a certain extension to communicate with auth0 and the extension can only work with data that is in array user_metadata. (I guessed - but not true). Anyway: If you could give me some lines of code would be nice - but not required :slight_smile:

If you did need to do something like this (we used to, when root attributes were not editable), you could:

function (user, context, callback) {
  user.user_metadata = user.user_metadata || {};
  if (!("given_name" in user.user_metadata)) {
    user.user_metadata.given_name = user.given_name;  
  }
  if (!("family_name" in user.user_metadata)) {
    user.user_metadata.family_name = user.family_name;
  }  

  auth0.users.updateUserMetadata(user.user_id, user.user_metadata)
    .then(function(){
        callback(null, user, context);
    })
    .catch(function(err){
        callback(err);
    });
}```
2 Likes

Thanks for sharing this solution Mark!