使用 cognito 用户池对 Graphql 进行身份验证

问题描述 投票:0回答:1

我需要帮助使用 Javascript/Nodejs 使用 Amazon Cognito 池连接到 Graphql(AWS AppSync) API。

我能够使用 API 密钥 (API_KEY) 作为身份验证机制连接到 Graphql。 但我需要将身份验证机制更改为使用 Cognito 池 (AMAZON_COGNITO_USER_POOLS)。

我知道我们需要收集 JWT 令牌并使用该令牌连接到 Graphql。但我不确定以下代码需要进行哪些更改。

我浏览了文档,但找不到任何对 Graphql 的 Cognito 身份验证的代码实现的引用。我已将“aws_appsync_authenticationType”更改为“AMAZON_COGNITO_USER_POOLS”。我不确定传递 JWT 令牌的参数名称(类似于“aws_appsync_authenticationType”)和实现。任何建议表示赞赏。

const { Amplify, API, graphqlOperation } = require('aws-amplify')

const awsConfig = {
    aws_project_region: 'us-east-1',
    aws_appsync_graphqlEndpoint: 'abc.com/graphql',
    aws_appsync_region: 'us-east-1',
    aws_appsync_authenticationType: 'API_KEY',
    aws_appsync_apiKey: '12345678',
    aws_cognito_identity_pool_id: 'abc123',
    aws_cognito_region: 'us-east-1',
    aws_user_pools_id: 'xyzpqr',
    aws_user_pools_web_client_id: '1a2b3c4d5e'
}

Amplify.configure(awsConfig)

API.graphql(graphqlOperation(query, parameter)).then(response => {
    console.log('response is ' + response)
}).catch(err => {
    console.log('erorr is ' + JSON.stringify(err))
})
javascript amazon-web-services graphql aws-amplify aws-appsync
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.