Auth0 Home Blog Docs

Unable to override name in custom hook or rule

#1

I am running custom registration and login forms (webAuth) and everything is working as expected, except that the “Name” field gets automatically assigned the user’s email address. I have attempted a few things so far to override the Name field, as I would like it to show their given name + last name instead. I have attempted using a post-registration hook, and putting the firstName and lastName (my app is written in javascript) field into user_metadata, and then trying to overwrite with
user.name = user_metadata.firstName + ' ' + user_metadata.LastName

This unfortunately did not work. I also attempted it inside of a rule, with similar code to no success.

I tried to follow the suggestion I found at this closed post: Why does a name property in user_metadata not override name in a returned id_token?

However as I mentioned, it simply will not allow me to override the name field on the user object.

Any suggestions?

Thanks!

#2

I should mention that it works in the little test/sandbox area of the post-register hook, adding the “name” field to the user object at the bottom and assigned it my desired values. However in production when I check my users, both in the GUI display their name shows up as their email address, as well as in the raw JSON.

I have a pre-register hook that is executing as desired so I believe I’ve configured this one correct, I am just prevented from adjusting the name.

#3

I also tried the suggestion at the following link for how to override user.picture, as it was suggested that I should be able to do the same for name but it didn’t work either:

Here is my rule below

function (user, context, callback) {
  user.user_metadata = user.user_metadata || {};  
  context.idToken['http://companyName.com/firstName'] = user.user_metadata.firstName;
  context.idToken['http://CompanyName.com/lastName'] = user.user_metadata.lastName;
  user.name = user.user_metadata.firstName + ' ' + user.user_metadata.lastName;
  
  callback(null, user, context);
}
#5

Hi @ayasso,

Welcome to the Auth0 Community!

Take a look at this article about normalized user profiles. Particularly look at this line ->

A user's `name` , `nickname` , and `picture` attributes are not directly editable, however you can update the fields in the `user_metadata` to update them for your front-end as desired.

Here is another document explaining why this is the case:

With that being said, I tested this rule and it will return the name field in the id token with the desired result.

function (user, context, callback) {
  if (user.user_metadata.firstName && user.user_metadata.lastName){
    user.name = user.user_metadata.firstName + " " + user.user_metadata.lastName;
  }
  callback(null, user, context);
}

Let me know if you have further questions.

Thanks,
Dan

closed #6

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