Set user metadata on registration from a react app

Hi,

I am using the React SDK to generate the registration link with loginWithRedirect and from there I would like to set metadata when the user registers. Is there a way to so?

Thank you

Hi @hello10,

Welcome to the Auth0 Community!

You can pass params to Auth0 from your React app like this:

loginWithRedirect({favorite_color:'blue'})

And you can access those params from post login actions like this:

event.request.query.favorite_color

You can set metadata in that action too, like this:

/**
 * @param {Event} event - Details about the user and the context in which they are logging in.
 * @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
 */
exports.onExecutePostLogin = async (event, api) => {
  if(event.request.query.favorite_color) {
      api.user.setUserMetadata("favorite_color", event.request.query.favorite_color);
  }
};
1 Like

Thank you. However my question was about regitration only, not login action and the query object is not available on registration actions.

You can use the login action to add it on first login. What is an example of the information so I can help you figure out how to add it?

@ dan.woda It is a referral flag, if the user registers when coming from a referral, then I want to add a flag to his profile, however, this should only happen on registration and not on subsequent logins

You can use the logins count stat to determine if it is a first login:

event.stats

Login statistics for the current user.

Includes the following properties:

logins_count Number. The number of times this user has logged in.

Here is the reference.

1 Like