Cannot update user info

I want to update a single user from my api.

I have a function in Express:

const updateUserAuth0 = async (req, res, next) => {
      var _token = await req.token.access_token;
      var apiToken = "Bearer " + JSON.stringify(_token).replace(/['"]+/g, "");

      var apiOptions = {
        method: "PATCH",
        url:
          "https://myurl.eu.auth0.com/api/v2/users/{" +
          new_user_id +
          "}",
        headers: {
          authorization: apiToken,
          "content-type": "application/json",
        },
        body: {
          family_name: "Newfamily",
        },
      };

      axios
        .request(apiOptions)
        .then(function (response) {
          req.allUsers = response.data.user_id;
          next();
        })
        .catch((err) => {
          console.log("ERROR RESPONSE", err.response.status);

          if (err.response.status === 409) {
            res.sendStatus(409);
          }
          next(err);
        });
    };

I call this function and pass it an access token with scopes “read:users update:users create:users read:users_app_metadata update:users_app_metadata create:users_app_metadata update:roles read:organizations update:organizations create:organization_members read:organization_members”

On the Auth0 Management API dashboard page on Machine to Machine Applications tab I clicked the down arrow on the API and updated to add the permissions on the access token.

On the MyAPI dashboard page on Machine to Machine Applications tab I clicked the down arrow on the API and updated to add the permissions on the access token.

On the user I am accessing myApi with, I allow all these permissions for myApi and the Auth0 Mgmt API

I have clicked and allowed everything available! I have no idea why I can’t change the user properties with the request as outlined in the mgmt api v2 docs

All I get is 400 - sometimes 404 as I change between slightly different syntax - what am I doing wrong?

Hi @melanie.alexandra,

Thank you for reaching out to Auth0 Community!

I noticed in your example the user_id appended the curly brackets in the URL, but instead, should follow the {identity provider id}|{unique id in the provider} format.

For example: if the user id is auth0|1234567890, the url should be like
https://myurl.eu.auth0.com/api/v2/users/auth0|1234567890

In this case, please change it to
url: "https://myurl.eu.auth0.com/api/v2/users/" + new_user_id,

Also,
body: { family_name: "Newfamily", }
should be
data: { family_name: "Newfamily", }

Please give it a try and keep us updated on how it goes. Thanks!

2 Likes

Thanks, I tried that and I can make it work. Can a user change their own user profile? How about if the user I want to change has google-oauth2 user id? I still get Bad Request when I log in with google-oauth2 and try to change my own profile, and I want to know the reason because I couldn’t find it in the docs?

Hi @melanie.alexandra ,

Thank you for the updates.

Can a user change their own user profile?

Yes, user can update their user profile from the hosting page. Developers can update the user profiles from the Management API.

How about if the user I want to change has google-oauth2 user id? I still get Bad Request when I log in with google-oauth2 and try to change my own profile, and I want to know the reason because I couldn’t find it in the docs?

Updating user profile data only works for users with “Username-Password-Authentications” connection, that is, users stored in Auth0 database. That’s why you received the error when trying with google-oauth2.

Any other queries we can assist with?

3 Likes