react-native-auth0: Bad HTTP authentication header format

We use react-native-auth0 and use Auth0 Management API User endpoints (new Users()). The description of the usage of this function in the document is below:

auth0
    .users('user token')
    .getUser({id: "user_id"})
    .then(console.log)
    .catch(console.error);

Description

Our code is below:

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

But it showing below error: Bad HTTP authentication header format

Prerequisites

Environment

Please provide the following:

  • React Native Auth0 version : ^1.2.2
  • React Native version : 0.54.4

Reproduction

Can anyone please explain the meaning of .users('user token'). I can’t understand how we send data in replace of user and token. The document is not cleared.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 20 (8 by maintainers)

Most upvoted comments

@MSSPL-PiyaleeMaiti In the scope you need to keep the openid value and include as well read:current_user, or the token won’t be granted access to that call.

@lbalmaceda It works for me. Working code is below:

emailIdLogin = () => {
    console.log('emailIdLogin', this.state.userName, this.state.password);
    auth0
    .auth
    .passwordRealm({
      username: this.state.userName,
      password: this.state.password,
      realm: "oh-development",
      scope : 'read:current_user openid',
      audience : `https://${Environment.AUTHDOMAIN}/api/v2/`,
    })
    .then((response) => {
      console.log('response-emailIdLogin', response);
      this.getUserInfo2(response);
    })
    .catch((err) => {
      console.log('error-emailIdLogin', err);
    });
  }

@lbalmaceda Thanks for your response. I already contact with support team. And with help of them, my code is working, and I get the proper response which I want. My code is below post, you can see. Thanks for your help.

@lbalmaceda Thank you. It’s working for me. My final code is below:

facebookLogin = () => {
    auth0
    .webAuth
    .authorize({
      scope : 'read:current_user openid',
      audience : `https://${Environment.AUTHDOMAIN}/api/v2/`,
      connection : 'facebook'
    })
    .then(credentials => {
      console.log('credentials-facebookLogin', credentials);
      this.getUserInfo2(credentials);
    })
    .catch(error => {
      console.log('error-facebookLogin', error);
    });
  }

  getUserInfo2 = (credentials = '') => {
    let self = this;
    if(credentials) {
      auth0
      .auth
      .userInfo({token: credentials.accessToken})
      .then((userInfo) => {
        console.log('getUserInfo2', userInfo);
        self.userUserInfo(credentials, userInfo);
      })
      .catch(err => {
        console.log('error-getUserInfo2', err);
      });
    }
  }

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

😄 👍 ❤️

@lbalmaceda sry, it’s my mistake. Now I correct my code and my code is below:

facebookLogin = () => {
    auth0
    .webAuth
    .authorize({
      scope : 'read:current_user',
      audience : `https://${Environment.AUTHDOMAIN}/api/v2/`,
      connection : 'facebook'
    })
    .then(credentials => {
      console.log('credentials-facebookLogin', credentials);
      this.getUserInfo2(credentials);
    })
    .catch(error => {
      console.log('error-facebookLogin', error);
    });
  }
getUserInfo2 = (credentials = '') => {
    let self = this;
    if(credentials) {
      auth0
      .auth
      .userInfo({token: credentials.accessToken})
      .then((userInfo) => {
        console.log('getUserInfo2', userInfo);
        self.userUserInfo(credentials, userInfo);
      })
      .catch(err => {
        console.log('error-getUserInfo2', err);
      });
    }
  }

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

Now I get error Error: invalid credentials. The error is shown when I call getUserInfo2 function and show error in the console log.

Instead of requesting audience https://${Environment.AUTHDOMAIN}/userinfo try using https://${Environment.AUTHDOMAIN}/api/v2/ (note the trailing slash) and in the scope include as well read:current_user. This way the token you get issued should allow you to call the users api.

@lbalmaceda My whole code will show you in below:

facebookLogin = () => {
    auth0
    .webAuth
    .authorize({
      scope : 'openid profile email',
      audience : `https://${Environment.AUTHDOMAIN}/userinfo`,
      connection : 'facebook'
    })
    .then(credentials => {
      this.getUserInfo2(credentials);
    })
    .catch(error => {
      console.log('error-facebookLogin', error);
    });
  }

getUserInfo2 = (credentials = '') => {
    let self = this;
    if(credentials) {
      auth0
      .auth
      .userInfo({token: credentials.accessToken})
      .then((userInfo) => {
        self.userUserInfo(credentials, userInfo);
      })
      .catch(err => {
        console.log('error-getUserInfo2', error);
      });
    }
  }

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

Below the whole code by which I want to get full user information. Now tell me which is wrong in my code.