Write Action To Add Metadata To A New User Profile

Will Johnson
InstructorWill Johnson
Share this video with your friends

Social Share Links

Send Tweet
Published a year ago
Updated a year ago

Add a custom field to the users metadata with the Pre User Registration trigger.

In the Actions editor enter the following code

exports.onExecutePreUserRegistration = async (event, api) => {
  api.user.setUserMetadata("favorite_ninja_turtle","raphael" )
};

This action uses the API object to add the new metadata before the user is saved to the database

To learn more about Auth0 user metadata read the docs here

Instructor: [0:00] On the Auth0 dashboard, let's go to user management and then go to users. I'll click on one of the users and I'll scroll down to user metadata. As you see, the section for user metadata is empty. [0:17] Let's write some code for an action that will add some metadata to the user's profile. On the Auth0 dashboard, go to actions then library. When you're on the library page, click build custom. Let's name the action add user metadata.

[0:44] Then we also have to select the trigger. For this, I will select pre-user registration. This is the trigger that runs before a user is added to the database. This would be the perfect time to add some profile information so that it's saved to the database with the new user. Click create. Now, you're brought to the actions editor.

[1:12] Inside of the editor, there is a exports.on execute pre-user registration function. The function that appears here will be different depending on which trigger that you choose. Notice that it's in async function and it also takes in an event and an API parameter.

[1:32] The event object provides contextual information about the request to register a new user and the API object provides methods for changing the behavior of the flow. To add some custom metadata, let's go into the function and let's go API.user.setusermetadata. As you see, you're able to get some auto complete with that, that's built into the actions editor.

[2:01] Inside of it, you'll pass in a string and for this, we'll do favorite Ninja Turtle. Favorite Ninja Turtle will be the name of the property and the value for favorite Ninja Turtle will be the only 1 answer which is Rafael. Now that you're satisfied with your action, click deploy.

[2:38] Once it's done being deployed, let's add it to the pre-user registration flow. We'll go to actions then flows, then click on pre-user registration.

[2:53] Then on the right-hand side, we will go to custom and we will drag the add user metadata to the pre-user registration flow, make sure that you click apply. On your sample React application, let's click login. Then we'll click sign up because we want to register a new user and I was like, one of my email addresses, make up a password really quickly.

[3:23] Then click continue. Make sure I will authorize the app with access. OK. So now we are logged in and we have a profile. Then I go back to the Auth0 dashboard, and I'll refresh my user's page, click on the user that I just created.

[3:45] Now, scroll down and see that the metadata for favorite Ninja Turtle, Raphael, has been added. I want you to keep in mind as of right now, you cannot add metadata to a user profile if they are registering using a Password-List connection.

egghead
egghead
~ 16 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today