Name from given_name and family_name

Hi Auth0 team and the community,

I am collecting given and family name during sign up and expect that “name” will contain full name of a user which is given_name + family_name but instead it always equals to email despite the fact that given_name and family_name are populater correctly.

Why is that and how to pupulate ‘name’ properly

  additionalSignUpFields: [{
    name: "given_name",
    placeholder: "Given name",
    storage: "root"
  },
  {
    name: "family_name",
    placeholder: "Family name",
    storage: "root"
  }]

Hi @ruslan,

When using the additional sign up fields, this data is entered into the user’s metadata, not the root profile (https://auth0.com/docs/libraries/lock/v11/configuration#additionalsignupfields-array-)

If you wanted to update the name property, you would need to follow the steps here to update the user’s root profile after they had signed up using a script or rule: https://auth0.com/docs/api/management/guides/users/update-root-attributes-users

If Auth0 isn’t the IDP in your use case, then some additional steps may be required -

Kind regards,
Steve

1 Like

Hi sgo,

In the link you sent, there is a paragraph

If you want to save the value of the attribute in the root of your profile, use storage: 'root' . Only a subset of values can be stored this way. The list of attributes that can be added to your root profile is here. By default, every additional sign up field is stored inside the user_metadata object.

and it works as expected - puts given_name and familiy_name in the root.

If you wanted to update the name property, you would need to follow the steps here to update the user’s root profile after they had signed up using a script or rule: https://auth0.com/docs/api/management/guides/users/update-root-attributes-users

Is there an example how to implemet it in a rule?

Appreciate your help.
Thanks

Hi @ruslan,
Yes you are correct, sorry I had forgotten that option had been added. However it appears the name attribute is not updated dynamically and must be manually overwritten in this scenario.

You can call the management API within a rule using the “managementClient”: https://auth0.com/docs/rules/guides/management-api

There is more detail here on how to use the managementClient here: https://github.com/auth0/node-auth0#management-api-client
To call the PATCH user endpoint, you could use this method: https://auth0.github.io/node-auth0/module-management.ManagementClient.html#updateUser

Here is an example rule using the above I made:

function (user, context, callback) {
  if (user.name === user.email){
console.log("Name is default, attempting change");
var ManagementClient = require('auth0@2.23.0').ManagementClient;
var management = new ManagementClient({
  token: auth0.accessToken,
  domain: auth0.domain
});
var givenName = user.given_name || '';
var familyName = user.family_name || '';
var userName = givenName+' '+familyName;
var data = {name: userName};
var params = { id: user.user_id};
management.updateUser(params, data, function (err, user) {
if(err){
  console.log(err);
  return callback(new Error("Could not update user name"));
}
// Updated user.
console.log("User updated:"+ user.user_id);
return callback(null, user, context);
  });
  } else {
console.log("Name change not required");
  return callback(null,user,context);
  }

}

You may want to change the logic, and add additional error handling where needed to suit your use case and sign up field validators config.

3 Likes

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