If the user_metadata object contains already other properties, these will be deleted (i.e. the user_metadata object is recreated).
I would like to keep the user_metadata content (if any) and eventually override specific properties.
Use case: in the social login connection, the fetchUserProfile is executed at each login (and this is the right behavior). After login, I would like to store other user attributes in the user profile user_metadata (f.e. using Actions) without worrying they will be deleted at the next login.
Here would go the root user attributes that are predefined in Auth0. These are: “user_id”, “email”, “given_name”, “family_name”, “name”, “nickname”, “picture”.
The User profile structure describes the above mentioned (1) root attributes you can update with the script and (2) metadata sub-objects that are not recommended to update via this script (most likely due to a reason you mentioned in this topic).
With actions, if you update only one metadata attribute, the other that already exists won’t be removed/overwritten.
Also one note worth mentioning:
Beware of storing too much data in the Auth0 profile. This data is intended to be used for authentication and authorization purposes, and users can edit their own user_metadata field, so don’t store sensitive data in it. The metadata and search capabilities of Auth0 are not designed for marketing research or anything else that requires heavy search or update frequency. Your system is likely to run into scalability and performance issues if you use Auth0 for this purpose. A better approach is to store data in an external system and store a pointer (the user ID) in Auth0 so that backend systems can fetch the data if needed.
Copied for a different topic, a sample code snipped for mapping root attributes, here mocked with hardcoded values (for your reference)
function(access_token, ctx, callback){
// call the oauth2 provider and return a profile
// here we are returning a "mock" profile, you can use this to start with to test the flow.
var profile = {
user_id: '123',
given_name: 'Eugenio',
family_name: 'Pace',
email: 'eugenio@mail.com'
};
callback(null, profile);
}