Signup via api: setting user metadata first name and last name

Hi there,

I’m using Auth0 + wordpress plugin to authenticate. This works fine.

For signups I use the api via http post request. I want to add user_metadata to the signup (first name and last name)…can anyone give me a hint how to create the post-call correctly to include user_metadata for firstname and lastname?

  1. When logging in the first time I want auth0 to hand over the user_metadata (first and last name ) to wordpress so the names are set correctly. How to I have to modify the rules so auth0 also sets first name and last name when creating the wordpress user during the first login?

Thanks!
Philipp

Hi @admin37,

Thanks for reaching out to the Auth0 Community!

I understand you have questions about setting the user’s user_metadata (first name and last name) when creating users.

  1. When using the Management API create a user’s endpoint, you can specify the user_metadata properties in the request body:
{
  "email": "john.doe@gmail.com",
  "connection": "Initial-Connection",
  "password": "secret",
  "user_metadata": {
      "first_name": "John",
      "last_name": "Doe "
  }
}
  1. There is an option to make a WordPress API call within an Auth0 Action to pass the user_metadata values. Moreover, you can check for the user’s logins_count to allow this to only happen for the user’s first successful login.

For example:

exports.onExecutePostLogin = async (event, api) => {
  if (event.stats.logins_count === 1) {
    var firstName = event.user.user_metadata.first_name
    var lastName = event.user.user_metadata.last_name
    //YOUR LOGIC HERE
  }
};

I hope this helps!

Please let me know how this works for you.

Thank you.

Hi @ rueben.tiow,

Could you please iterate on the second step. We added the method onExecutePostLogin as Custom Action into Actions / Login.

Where your script says //YOUR LOGIC HERE do we have to call the WordPress API and write the users metadata to WP? At this point we don’t even know the WP userid to call an update on the user model, right? Also there is no way to search for a user by email through WP API?

Is there already an active connection through the Auth0 WordPress Plugin? Or do we have to call the API as regular from an outside script?

Thanks

1 Like

Hi @admin37,

Thank you for your reply.

Yes, this would be the section where you call the WP API to write the user’s metadata to WP by passing the firstName and lastName variables in the request.

I just reviewed the WP API Documentation and agree that there is no way to search for a user by email.

That being said, as a workaround, I recommend storing the user_id in the user_metadata as part of Step 1. This way, you can make the POST request to update the user in Step 2.

I believe so. You should be able to determine this by checking where the Connection name of where the user belongs on the Auth0 Dashboard > User Management > Users page.

I hope this answers your questions. Please let me know if there’s anything else I can do to help.

Thank you.