How to add user_metadata to profile from login?

I have added the following user_metadata
{
“memberID”: 241
}

Then I added a rule using the “Move user metadata attributes to profile root attributes” template and modified the fieldMapping variable below to map memberID to a new root attribute named memberID. However, when I view the user profile on the client after login in the debugger (see below) the “memberID” attribute and value from user_metadata is not present. Does the rule have to be attached to an application or something to be active?

function (user, context, cb) {
// Field Mapping, the property is the root attribute and the value is the field name on user_metadata.
// You can change the value in case you don’t have the same name on user_metadata.
var fieldMapping = {
memberID: ‘memberID’
};

if (needMigration(user)) {
var ManagementClient = require(‘auth0@2.9.1’).ManagementClient;
var management = new ManagementClient({
domain: auth0.domain,
token: auth0.accessToken
});

management.updateUser(
  { id: user.user_id }, generateUserPayload(user), function (err, updatedUser) {
    if ( err ) {
      cb(err);
    } else {
      updateRuleUser(user, updatedUser);
      cb(null, user, context);
    }
  }
);

} else {
cb(null, user, context);
}

function needMigration(user) {
if (user.user_metadata) {
for (var key in fieldMapping) {
if (typeof user.user_metadata[fieldMapping[key]] === ‘string’) {
return true;
}
}
}

return false;

}

function generateUserPayload(user) {
var payload = { user_metadata: {}};
var userMetadata = user.user_metadata;

for (var key in fieldMapping) {
  generateUserPayloadField(userMetadata, payload, key, fieldMapping[key]);
}

return payload;

}

function updateRuleUser(user, updatedUser) {
for (var key in fieldMapping) {
if (typeof user.user_metadata[fieldMapping[key]] === ‘string’) {
user[key] = updatedUser[key];
delete user.user_metadata[fieldMapping[key]];
}
}
}

function generateUserPayloadField(userMetadata, payload, rootField, metadataField) {
if (typeof userMetadata[metadataField] === ‘string’) {
payload[rootField] = userMetadata[metadataField];
payload.user_metadata[metadataField] = null;
}
}
}

User profile on client

  1. aud: “aXZtTX9fy…”
  2. exp: 1574924095
  3. iat: 1574888095
  4. iss: “https://mXXXX.auth0.com/
  5. name: “Chris Pels”
  6. nickname: “chris”
  7. nonce: “xKOg_uM4Fpsw…”
  8. picture: “https://s.gravatar.com/avatar/4a2b36c1f95b531dd0c6492f1ec9d6c0?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fch.png
  9. sub: “auth0|5dcecc3…”
  10. updated_at: “2019-11-27T20:46:53.045Z”

Hello @treasurer,

tl;dr: You can’t create arbitrary root attributes. You can only edit (some of) them. You can add attributes from user_metadata to your idToken.

Background: Previously attributes like family_name were not editable. Since names do change this made the root level family_name attribute, and others like it, not very useful. So many of us started creating our own name attributes in user_metadata.

Relatively recently, Auth0 made more of the root attributes editable, including the name attributes. The purpose of that rule template you are using is just to make life easier for Auth0 customers who want to migrate their custom attributes into the (now editable) equivalent root attributes. But this only works for the root attributes that exist. It is not possible to create new ones.

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