Amplify API订阅失败授权

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

我有一个当前使用 Amplify GraphQL API 的 React 应用程序。我已经设置了这些 API,以便它们需要使用 Cognito 用户池进行授权。例如,以下代码成功地从我在 DynamoDB 中的项目集合中检索值列表

const apiData = await API.graphql({ query: listProjects, authMode: "AMAZON_COGNITO_USER_POOLS"  });

最近我一直在尝试设置订阅,以便在新条目添加到我的项目集合时,我可以从 AppSync 接收实时更新。这是我要开始工作的代码:

const subscription = API.graphql({query: subscriptions.onUpdateProject, authMode: "AMAZON_COGNITO_USER_POOLS"}
    ).subscribe({
    next: ({ provider, value }) => console.log({ provider, value }),
    error: error => console.warn(error)
});

不幸的是,此代码在执行时失败,导致出现此错误:

"Connection failed: {\"errors\":[{\"errorType\":\"Unauthorized\",\"message\":\"Not Authorized to access onUpdateProject on type Project\"}]}"

我不太确定如何排除或解决此问题,订阅是否需要在授权方面进行特殊配置?订阅是否支持 Cognito 用户池?任何帮助或指导将不胜感激。

reactjs aws-amplify aws-appsync
2个回答
1
投票

我能够通过添加一个变量来解决这个问题,以指定我的订阅的所有者,如下所示。我在另一个问题中发现了这一点(https://github.com/aws-amplify/amplify-category-api/issues/456)。

        const updateProjectSubscription = API.graphql({query: subscriptions.onUpdateProject, authMode: 'AMAZON_COGNITO_USER_POOLS', variables: {
            owner: Auth.user.attributes.sub 
        }}


0
投票

这对我有用-将

AuthModeStrategyType.MULTI_AUTH
添加到配置中:

https://docs.amplify.aws/lib/datastore/setup-auth-rules/q/platform/js/#configure-multiple-authorization-types

import { Amplify, AuthModeStrategyType } from 'aws-amplify';
import awsconfig from './aws-exports'; 

Amplify.configure({
  ...awsconfig,
  DataStore: {
    authModeStrategyType: AuthModeStrategyType.MULTI_AUTH
  }
})
© www.soinside.com 2019 - 2024. All rights reserved.