【发布时间】:2016-09-27 13:21:53
【问题描述】:
我正在使用 Auth0 的 react-native-lock 小部件进行用户身份验证,并在成功登录后使用 AsyncStorage 存储令牌。
如果用户返回应用程序,我希望能够跳过登录并简单地从 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});
}
}
我错过了什么?我找不到很多将 fetch 与 Auth0 一起使用的示例,我应该使用更好的方法吗?
【问题讨论】:
标签: react-native auth0 asyncstorage