Amplify 缺少 Cognito 自定义声明,但 Appsync 控制台没有

lep*_*thy 3 claims-based-identity custom-attribute amazon-cognito aws-appsync aws-amplify

我有以下解析器,允许我检索有关当前用户公司的信息(companyId 作为自定义字段添加到 cognito 用户池中)。cognito 上的字段设置为可变的。

{ "version" : "2017-02-28", "operation" : "GetItem", "key": { "id" : $util.dynamodb.toDynamoDBJson($context.identity.claims.get("custom:companyId")) } }

——

这在使用 AWS AppSync 界面(登录后)时工作正常,如日志所示:

{ "errors": [], "mappingTemplateType": "Request Mapping", "path": "[getMyClientCompany]", "resolverArn": "arn:aws:appsync:eu-west-1:261378271140:apis/rue25cac6jc6vfbhvu32sjafqy/types/Query/fields/getMyClientCompany", "transformedTemplate": "{\n \"version\" : \"2017-02-28\",\n \"operation\" : \"GetItem\",\n \"key\": {\n \"id\" : {\"S\":\"0c1c81db-a771-4856-9a30-d11bf8e3cab1\"}\n }\n}", "context": { "arguments": {}, "source": null, "result": null, "error": null, "outErrors": [] }, "fieldInError": false }

——

但是当代码来自 Amplify-js 时不起作用:

{ "errors": [], "mappingTemplateType": "Request Mapping", "path": "[getMyClientCompany]", "resolverArn": "arn:aws:appsync:eu-west-1:261378271140:apis/rue25cac6jc6vfbhvu32sjafqy/types/Query/fields/getMyClientCompany", "transformedTemplate": "{\n \"version\" : \"2017-02-28\",\n \"operation\" : \"GetItem\",\n \"key\": {\n \"id\" : {\"NULL\":null}\n }\n}", "context": { "arguments": {}, "source": null, "result": null, "error": null, "outErrors": [] }, "fieldInError": false }

应该是“custom:companyId”的关键是“NULL”,现在我想问题出在 Amplify(0.4.8 版)或出于某种原因的认知用户解析器

知道会发生什么吗?

Mar*_*nko 5

Cognito 可以使用两个 JWT 令牌。身份和访问权限。ID 令牌似乎包含这些自定义声明。

在 Amplify 中,您可以调整 Authorization 标头以使用 ID 令牌与访问令牌。

这是代码,将其放入 AWS Amplify 配置中:

API: {
  graphql_endpoint: 'https://****.appsync-api.***.amazonaws.com/graphql',
  graphql_region: '***',
  graphql_authenticationType: 'AMAZON_COGNITO_USER_POOLS',
  graphql_headers: async () => {
    try {
      const token = (await Auth.currentSession()).idToken.jwtToken;
      return { Authorization: token }
    }
    catch (e) {
      console.error(e);
      return {};
      // Potentially you can retrieve it from local storage
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,似乎有几个不同的键可以配置 Amplify 键:例如, aws_appsync_graphqlEndpointvs API { graphql_endpoint },我使用了后者。