2016-05-29 45 views
0

我正在使用Auth0的react-native-lock小部件进行用户验证,并且在成功登录后,我将存储令牌与AsyncStorage。在React Native中获取来自Auth0应用程序的userinfo

如果用户回到应用程序,我希望能够跳过登录并简单地从Auth0获取当前用户信息。这似乎从该Auth0 API docs非常容易的,但我在我的应用程序取回消息“未经授权”:

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}); 
} 
} 

我缺少什么?我找不到很多使用Auth0提取的例子,有没有更好的方法可以使用?

回答

0

an associated GitHub issue范围内发现的问题。如果有人在这里,而不是那里的土地问题得到有效解决,问题是,你需要使用:

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

原始代码试图将一个字符串字面'Bearer ${this.state.token.accessToken}'内访问变量。

相关问题