Pre registration hook set non-metadata user info?

Hi,

We are in the process of migrating from another user database and have a custom database setup for this purpose. The login script there defines some user fields that don’t seem to be available to set from a hook script.

So, how do I set user fields outside app_metadata and user_metadata from a pre registration hook script?

I tried this:

var body = // Add user to old system and get data from there.
response.user.user_id = 'OldSystem|' + body.UserId;
response.user.nickname = body.FullName;
response.user.username = body.UserName;
response.user.given_name = body.FirstName;
response.user.family_name = body.LastName;

But none of these fields actually get set in the resulting Auth0 user object. The field nickname seems to be assigned the part before @ in the new user’s e-mail address.

The hook script does assign some app_metadata fields, which do get stored properly in Auth0’s new user object.

Is there any way to set those fields from the hook script?

As a last resort, would it be possible to add also a post registration hook script and let that set those Auth0 user fields? In that case, hos would such a script do that?

I should add that some apps still use the old system for user auth, so we need all new user registrations to be made to both Auth0 and the old system. That’s why the pre registration hook script saves the new user to the old system. It also retrieves the old-system user id and saves it to Auth0, so applications can login via Auth0 but be able to make API calls to the old system using that old-system user id to identify the user to the old system.

It does seem somewhat odd that the custom database login script and the pre reg hook script has different “access” to non-metadata parts of the Auth0 user object.

1 Like

Hi there @krilbe,

I wanted to reach out and let you know I am looking into this. I will relay what I find :+1:

Hi @James.Morrison,

Any news on this? We stumbled upon the same issue and can’t find any other available docs for this.

Thanks,
Adela

Thanks for resurfacing this @aneacsu_c, I’ll follow up on this front with our team.

After talking with a senior engineer on this front @aneacsu_c and @krilbe, can you share your current pre-registration hook so we can take a look at the code you are working with? We also wanted to ask if the overall goal is to set root attributes? We have documentation on that front below that may help shed some light on how to accomplish that.

If by chance you are looking to migrate from a legacy system and import the data, you can leverage an automatic migration through custom scripts like described below.

Please let me know if this helps you in your pursuits or if you have any questions, thanks!

@James.Morrison, this is my current hook code:

module.exports = function (user, context, cb) {
  let response = {};
  response.user = user;

  response.user.given_name = user.user_metadata.firstName;
  response.user.family_name	= user.user_metadata.lastName;

  // Some more code here...

  console.log(response.user);
  cb(null, response);
};

I’m receiving the data on sign-up, and creating a user for which the name is also mandatory. Auth0 is the IDP. The data is sent through user_metadata.

I can see from the docs you posted that this can be done if I send them directly as family_name and given_name under user?

Currently, these attributes are not being set.

When you get a chance @aneacsu_c can you send the tenant name in a direct message that you are currently working with? Thanks!

Here I am experiencing the same issue with the persistence after pre-registration hook.
Looks like user_metadata keys added to the hook’s response are not available after (from a rule or on the user in general).
On the hook I get a result with the user.user_metadata expected keys and valuse. I am validating it both from the hook webex tester as shown:

And also from the Real-time Webtask Logs extension when trying with from my client:

But, on the client response, and on the rules and on the user manage dashboard I cant see the user.user_metadata:

Screen Shot 2020-01-28 at 7.53.41 PM

Hi there @guy1812, can you direct message me your tenant name as well? Thanks in advance!

Just PMed you with the tenant :slight_smile:
Also for easier reproduce, I removed all the logic from the hook and left only:
Promise.resolve then response.user.user_metadata = { name: ‘test’ }
You can still see that the webcode simulator shows a response with the user_metadata but it is not shown when checking the user profile

Hi @guy1812,

Can you post your hook in its entirety(omitting sensitive data of course)?

I got the metadata to persist when I used the example code. There may be something more going on here.

Thanks,
Dan

My bad, I was playing around and persisted metadata from a rule. Until you will answer I will freeze my activity on the platform.
From rules it works well, but not from the hook
Here is a video where you can see it better:

@guy1812,

I think I wasn’t clear enough, apologies. Could you please post the code for the hook in its entirety.

Thanks!

module.exports = function (user, context, cb) {
  var response = {
    user: user,
  };
  var body = {data: JSON.stringify({user, context}, null, 2)};
  console.log('secrets', context.webtask.secrets);
  
  return Promise.resolve({name: 'test'})
    .then(json => {
      response.user.user_metadata = { name: json.name };
      console.log('hook response.user', response.user);
      cb(null, response);
    })
    .catch(error => {
      console.log(error)
      cb(error);
    });
};
1 Like

I have also validated myself and tried to remove the pre-reg-hook and create a new one with the code:

module.exports = function (user, context, cb) {
  var response = {};
  response.user = user;
  response.user.user_metadata = {a: 'test'}
  cb(null, response);
};

And the user_metadata is not shown on the user page.
I wonder what am I missing…
Thanks for the help.

@guy1812

Both of these hooks worked for me :thinking:.

There must be another config that is interfering. Do you have any other hooks? How about anything else that is changing metadata?

Thanks! I don’t know what to do next, No other hooks nor rules. Can you go into my tenant (I am PMing you the tenant) and check?


@guy1812,

I have narrowed this down a bit. I can get everything working fine with a normal db connection, but it will not work with my passwordless sms connection. I am starting to think this is a bug and not a misconfig. Thanks for your patience on this.

After more testing I am fairly sure this is a bug. I submitted it to the team for review.

4 Likes

yep it is a bug - on passwordless, user_metadata is not persisted on pre-reg hook.
Thanks @dan.woda!

2 Likes