amazon-dynamodb - 带有 ctx.identity.username 的简单 GetItem 返回 null

标签 amazon-dynamodb amazon-cognito aws-appsync aws-amplify vtl

我将 AppSyncIAM auth 以及 DynamoDB 解析器Cognito 结合使用。我正在尝试执行以下操作。

{
    "version": "2017-02-28",
    "operation": "GetItem",
    "key": {
        "userId": $util.dynamodb.toDynamoDBJson($ctx.identity.username)
    }
}

$ctx.identity.username 应该包含由 Cognito 生成的 userId ,我正在尝试使用它来获取当前用户数据。

客户端,我使用 AWS Amplify 来告诉我当前已登录:

this.amplifyService.authStateChange$.subscribe(authState => {
  if (authState.state === 'signedIn') {
    this.getUserLogged().toPromise();
    this._isAuthenticated.next(true);
  }
});

getUserLogged 是应该返回用户数据的 Apollo 查询

我尝试过的:

  • 如果我像这样保留它,getUserLogged 将返回 null。
  • 如果我在解析器中将 $util.dynamodb.toDynamoDBJson($ctx.identity.username) 替换为已知的 userId,如下所示 $util.dynamodb .toDynamoDBJson("b1ad0902-2b70-4abd-9acf-e85b62d06fa8"):它有效!我获取了这些用户数据。
  • 我尝试在解析器页面中使用测试工具,但它只提供虚假数据,因此我不能依赖它。

我是不是搞错了?对我来说一切看起来都不错,但我想我错过了一些东西?

我可以清楚地看到$ctx.identity包含什么吗?

最佳答案

您需要使用 $ctx.identity.cognitoIdentityId 来识别 Cognito IAM 用户: https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference.html#aws-appsync-resolver-context-reference-identity

您可以通过创建 Lambda 解析器并记录事件或通过创建本地解析器并返回映射模板收到的输入来查看 $ctx.identity 的内容: https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-local-resolvers.html

我的 cognitoIdentityId 如下所示:eu-west-1:27ca1e79-a238-4085-9099-9f1570cd5fcf

关于amazon-dynamodb - 带有 ctx.identity.username 的简单 GetItem 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51230159/

相关文章:

amazon-web-services - 通过 CloudFormation 创建 AWS::ApiGateway::Method 集成时 ARN 无效

python - boto3 DynamoDB - 查询操作 : ExpressionAttributeNames contains invalid key

aws-amplify - 无权访问 IFreightDriver 类型上的 id

aws-cloudformation - 应用同步 : Get user information in $context when using AWS_IAM auth

amazon-dynamodb - AWS AppSync GraphQL 按字段值查询记录

node.js - 如何使用 AWS.DynamoDB.DocumentClient 在 ExpressionAttributeValues 中表达数字类型

amazon-dynamodb - 离线启动无服务器时出现 ENOENT 错误

javascript - x-amzn-错误类型 :UnrecognizedClientException While Calling AWS Api gateway with temporary Credentials

ios - Google Amplify iOS 应用程序 -> NSUnknownKeyException

java - 使用 Cognito SDK 时为什么需要提供 AWS 凭证?