【问题标题】:How can I retrive the AWS Cognito user from from a valid access token using aws-amplify library?如何使用 aws-amplify 库从有效访问令牌中检索 AWS Cognito 用户?
【发布时间】:2021-06-08 21:52:36
【问题描述】:

我只想使用用户之前登录时生成的有效 jwt 访问令牌来检索 Cognito 用户属性和其他详细信息。

我使用 Nodejs,想知道是否可以使用 aws-amplify 库来做到这一点。

【问题讨论】:

    标签: amazon-cognito aws-amplify


    【解决方案1】:

    我们可以使用像jwt-decode 这样的第三方库来解码 JWT。有效负载本身包含详细信息。

    【讨论】:

      【解决方案2】:

      aws-amplify 库中有一个方法可以用于此目的。

      import { Auth } from 'aws-amplify';
      
      Auth.currentAuthenticatedUser({
          bypassCache: false  // Optional, By default is false. If set to true, this call will send a request to Cognito to get the latest user data
      }).then(user => console.log(user))
      .catch(err => console.log(err));
      

      您将在响应的负载中看到attributes 字段。

      如果遇到任何问题,请查看官方documentation

      此方法可用于在页面加载时检查用户是否登录。如果没有用户登录会报错。这个方法应该在配置Auth模块或者用户登录后调用。

      【讨论】:

      • 我想从 JWT 令牌中检索用户。不是来自 currentAuthenticatedUser。
      • 如果您使用的是 aws-amplify 包,则无需存储 JWT 令牌和处理刷新令牌逻辑等。如果您使用 currentAuthenticatedUser 方法,aws-amplify 会为您完成这些。此外,您还询问了一种使用 aws-amplify 包获取用户属性的方法。
      猜你喜欢
      • 2018-08-10
      • 2019-09-01
      • 2016-06-10
      • 1970-01-01
      • 2019-06-22
      • 2019-07-28
      • 2018-06-07
      • 2021-10-15
      • 2020-10-27
      相关资源
      最近更新 更多