首页 文章

使用Amplify时,Cognito自定义声明丢失,但与Appsync控制台无关

提问于
浏览
1

我有以下解析器,允许我检索有关当前用户公司的信息(companyId被添加为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)或者是出于某种原因的cognito用户解析器

知道会发生什么吗?

1 回答

  • 2

    Cognito可以使用两种JWT令牌 . ID和访问权限 . ID令牌似乎包含那些自定义声明 .

    从Amplify中,您可以调整Authorization标头以使用ID令牌与Access令牌 .

    这是代码,把它放在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
        }
      }
    }
    

    注意,似乎有几个不同的键来配置Amplify键:例如, aws_appsync_graphqlEndpoint vs API { graphql_endpoint } ,我使用后者 .

相关问题