Skip to content
This repository has been archived by the owner on Jul 18, 2020. It is now read-only.

Using accessToken for API, userinfo? #52

Closed
allpwrfulroot opened this issue May 29, 2016 · 4 comments
Closed

Using accessToken for API, userinfo? #52

allpwrfulroot opened this issue May 29, 2016 · 4 comments
Labels

Comments

@allpwrfulroot
Copy link

allpwrfulroot commented May 29, 2016

After a user logs in, I'm storing the tokens via AsyncStorage and trying to fetch the user's profile upon re-visiting the app.
This appears to be super simple in 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?

@hzalaz
Copy link
Member

hzalaz commented May 30, 2016

@allpwrfulroot does this 'Bearer ${this.state.token.accessToken}' work?
Shouldn't it be like this

`Bearer ${this.state.token.accessToken}`

?

@allpwrfulroot
Copy link
Author

Close, this seems to work:

     let response = await fetch('https://xxxxxxxxx.auth0.com/userinfo', {
        method: 'GET',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
          Authorization: 'Bearer ' + this.state.token.accessToken,
        },
      });
      let responseJson = await response.json();
      if(response !== null) {
      ...

@hzalaz
Copy link
Member

hzalaz commented Jun 6, 2016

@allpwrfulroot glad it work but probably you where confusing the apostrophe ' with the back-tick ` since it's the character used for String templates in ES6

@Kozlo
Copy link

Kozlo commented Dec 7, 2016

Maybe in your case it's not applicable, but in the docs I found that one should call the 'getUserInfo' on the lock an simply pass the accessToken and the callback as args:
https://github.com/auth0/lock

Like this:
lock.on("authenticated", function(authResult) {
lock.getUserInfo(authResult.accessToken, function(error, profile) {
if (error) {
// Handle error
return;
}

localStorage.setItem("idToken", authResult.idToken);
localStorage.setItem("profile", JSON.stringify(profile));

// Update DOM

});
});

But I suppose it depends on how you're using Auth0. If you're not using the Lock or you want to get the info without the lock, it doesn't apply here.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants