![](/img/trans.png)
[英]AWS Amplify AppSync Login using Google user that's created in Cognito
[英]Cognito custom claims missing with Amplify but not with Appsync Console
我有以下解析器,允許我檢索有關當前用戶公司的信息(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用戶解析器
知道會發生什么嗎?
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 }
,我使用后者。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.