React-native-auth0: How to call the users Method to patch user's metadata

Created on 18 Apr 2018  路  12Comments  路  Source: auth0/react-native-auth0

Tried to patch/ edit user on auth0 but I'm getting a Bad HTTP

auth0
.users(this.state.token)
.patchUser({id: this.state.id, metadata: {'first_name': 'John', 'last_name': 'Doe'}})
.then(results => console.log(results))
.catch(error => console.log(error));

documentation

Most helpful comment

We did something like this.

const { sub, email } = await auth0.auth.userInfo({
  token: auth.accessToken,
});

const managementAuth = await auth0.auth.passwordRealm({
  username: email,
  password: password,
  realm: 'Username-Password-Authentication',
  audience: 'your auth url',
  scope: 'read:current_user update:current_user_metadata',
});

await auth0.users(managementAuth.accessToken).patchUser({
  id: sub,
  metadata: { givenName, familyName },
});

Hope this helps

All 12 comments

This will be closed unless more information is provided. Thx

This happens for me too. What information do you need? For me it happens with both patchUser and getUser. I compared a working request, userInfo, with getUser and the header seems exactly the same. This is the header where i get the Bad HTTP auth. response:

'GET',
'https://radinn.eu.auth0.com/api/v2/users/google-oauth2%xxxxxxxxxxxxxxxxxxxxx',
{
  method: 'GET',
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
    'Auth0-Client': 'xxxxxxxxxxxxx',
    Authorization: 'Bearer xxxxxxxxxxxx'
  }
}

I think Im probably using the wrong 'user-token'. It seems as if management api is not suppose to be used my client applications? I want to add meta-data when a user logs in and it would be simple to add a call to patchUser in my app...

You should be using the idToken. The sub from the UserInfo profile should be used for the id.

I tried that and then I get:

StatusCode: 401,
 error: 'Unauthorized',
message: 'Invalid token',
attributes: { error: 'Invalid token' } 

If I use the token from API explorer, it works.
I don't understand what I need to to do to make idToken from userInfo work though?

Sorry, had my legacy hat on.
You need to specify the Management API as the audience when you authenticate to obtain an AT (access token) with permission to call this endpoint.

So if you look in Management APIs you will have https://<YOURDOMAIN>/api/v2/ this will be your audience.

You also need to request in your scopes the access you need. For example to be able to read/update the current user you can ask for (in addition to the standard openid profile scopes)

read:current_user update:current_user_metadata

You will also use the AccessToken not the IdToken in the Users call.

We did something like this.

const { sub, email } = await auth0.auth.userInfo({
  token: auth.accessToken,
});

const managementAuth = await auth0.auth.passwordRealm({
  username: email,
  password: password,
  realm: 'Username-Password-Authentication',
  audience: 'your auth url',
  scope: 'read:current_user update:current_user_metadata',
});

await auth0.users(managementAuth.accessToken).patchUser({
  id: sub,
  metadata: { givenName, familyName },
});

Hope this helps

Auth0 caches the user profile, so this patch only shows after user logs in again. How did you solve this problem? Any way to disable or delete the cache?

@VilleMiekkoja The results object for the patch should contain a complete profile with the changes applied.

Can anyone tell me how I use .getUser method? An example can show for usage. I give Bad HTTP authentication header format this error. Can't understand why this is showing?
@bitshadow I follow ur code but I get below error:
Bad HTTP authentication header format
My code is below:

      .users(credentials.accessToken)
      .getUser({id: userInfo.sub})
      .then((fullUserInfo) => {
        console.log('userUserInfo', fullUserInfo);
      })
      .catch(err => {
        console.log('error-userUserInfo', err);
      });

https://github.com/auth0/react-native-auth0/issues/197#issuecomment-452997063
Here is the link where shown that proper solution to use that method. And it's working.

Was this page helpful?
0 / 5 - 0 ratings