我有以下解析器,允许我检索有关当前用户公司的信息(公司 ID 作为自定义字段添加到 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 用户解析器有关
知道会发生什么吗?
Cognito可以使用两个JWT令牌。ID 和访问权限。ID 令牌似乎包含这些自定义声明。
在 Amplify 中,您可以调整授权标头以使用 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
}
}
}
请注意,似乎有几个不同的键来配置放大键: 例如,aws_appsync_graphqlEndpoint
vsAPI { graphql_endpoint }
,我使用了后者。