0
votes

I'm using Auth0's react-native-lock widget for user authentication, and after a successful login I'm storing the tokens with AsyncStorage.

If a user comes back to the app, I'd like to be able to skip the login and simply fetch the current userinfo from Auth0. It seems incredibly easy from the the Auth0 API docs but I'm getting back the message "Unauthorized" in my app:

async getUserinfo() {
console.log('getting user info in getUserInfo()');
try {
  let response = await fetch('https://xxxxx.auth0.com/userinfo', {
    method: 'GET',
    headers: {
      Authorization: 'Bearer ${this.state.token.accessToken}',
    },
  });
  let responseJson = await response.json();
  if(responseJson !== null) {
    console.log('Got user info: ' + responseJson.email);
    this.setState({ component: Temp, isLoading: false, profile: responseJson});
  }
} catch (error) {
  console.log('Error in retrieving userinfo from Auth0: ' + error.message);
  this.setState({ component: Login, isLoading: false});
}
}

What am I missing? I can't find many examples of using fetch with Auth0, is there a better method I should be using?

1

1 Answers

0
votes

Problem identified in the scope of an associated GitHub issue. In case anyone lands here and not where the issue got solved, the problem is that you need to use:

Authorization: 'Bearer ' + this.state.token.accessToken, 

Original code tried to access the variable within a string literal 'Bearer ${this.state.token.accessToken}'.