Auth0 Home Blog Docs

Set user_metadata property on signup hook

Hi, I need to assign an extra user property (being provided by a HTTP call) to a users meta data at signup by setting up a Post User Registration hook that looks like this:

module.exports = async function (user, context, cb) {
  const ax = require('axios')
  const requestBody = {
                firstName: user.user_metadata.firstName,
                lastName: user.user_metadata.lastName,
                emailAddress: user.email
            }
  const url = '*URL*';
  const options = {
      method:'post',
      headers: {
          'Content-Type': 'application/json'
      }
  }
  result = await ax.post(url, requestBody, options);  
  
  var response = {};
  response.user = {
    user_metadata: { 
      MyExtraProperty: result.MyProperty
    }
  };

  cb(null, response);
};

Firstly, this code does not seem to wait for the response as the response I’m getting is

{
  "user": {
    "user_metadata": {}
  }
}

Secondly, is a post user registration hook the right place for this? Will I be able to apply this property to the users metadata at this point?

Thirdly, do I need to create this metadata field as part of user objects or will it be implicitly created once being set like this?

Thanks in advance

The user_metadata does not get persisted like this when used in a post-registration hook. Code above would work fine in a pre-registration hook though, because that’s being called before the user gets persisted (incl. your changes made to the user_metadata).

In your above case though, you add user_metadata to the already persisted user, so that’s just a volatile change.

Assuming that the pre-registration hook is not an option for you, you would need to handle the persistence call manually in the post-registration hook. You would need to call the Management API to persistent the change, in particular: https://auth0.com/docs/api/management/v2#!/Users/patch_users_by_id because unfortunately in Hooks, there is no helper object present as it is in rules (where you have an auth object (which is an instance of the node-auth0 SDK).

1 Like

Thanks for your reply. Using the pre-registraion hook should be OK as long as I can use the user_metadata fields that have been added to the signup process in my call. Problem is ‘result’ doesn’t seem to contain the response body (JSON object) from the call I’m making so when testing the code with:

cb(null, result.MyProperty);

  • the response shows as {}.

This looks like possibly an async issue as the test execution completes before my call has finished processing.

Just to tidy this up, I’ve gone with a different approach using callbacks:

module.exports = function (user, context, cb) {
  const axios = require('axios');  
  const requestBody = {
                firstName: user.user_metadata.firstName,
                lastName: user.user_metadata.lastName,
                emailAddress: user.email
  }  
  const url = '*URL*';
 
  axios({
    url: url,
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data: requestBody
  }).then((result) => {
	// add my property to the user_metadata
    var response = {};
    response.user = user;
    response.user.user_metadata.MyProperty = result.data.MyProperty;
    
    cb(null, response);
    }).catch(function (error) {
      console.log(error);
    });
};

My call returns a JSON body:

{
...
  "MyProperty": "value"
...
}

This now successfully assigns the property I wanted to include in the users user_metadata upon registration.

1 Like

Thanks a lot @stratman82 for sharing it with the rest of community!

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