【问题标题】:Fetching userinfo from Auth0 application in React Native从 React Native 中的 Auth0 应用程序获取用户信息
【发布时间】: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


    【解决方案1】:

    an associated GitHub issue 范围内发现的问题。万一有人来到这里而不是问题得到解决的地方,问题是你需要使用:

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

    原始代码试图访问字符串文字'Bearer ${this.state.token.accessToken}'中的变量。

    【讨论】:

      猜你喜欢
      • 2019-05-08
      • 2018-09-03
      • 2018-08-07
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      相关资源
      最近更新 更多