AWS amplify - 如果我使用 API 密钥,则无法通过 DataStore 进行同步;但 Cognito 用户池有效

AWS amplify - Can't synchronize via DataStore if I use an API Key; but Cognito User Pools work

我已经为 react.js 设置了一个放大项目,它应该允许对我的应用程序进行授权和未授权访问。 因此,我配置了两种身份验证方法:Cognito 用户池和 API 密钥。第一个是我的默认值。 如果我通过 Cognito 用户池登录,我的应用程序运行良好。所有数据都通过 DataStore 同步。 但是,如果我以未经身份验证的用户身份切换到 API 密钥,我只会在后台遇到未经授权的错误。例如。对于我的自定义类型之一:

errorType: "Unauthorized"
message: "Not Authorized to access onCreateMyCustomType on type Subscription"

API 密钥被正确发送为“x-api-key”。

如果我手动查询 Graph-API(例如通过我的应用程序中的 amplify.js-API 或第三方工具 GraphiQL),我可以得到结果。只是同步对经过身份验证的用户不起作用。

在我的模式中,我通过@auth 控制访问。例如:

@auth(rules: [
    # Owner access
    { allow: owner },

    # System access
    { allow: private, provider: iam },

    # Admin access
    { allow: groups, groups: ["Admin"] },

    # Default user access
    { allow: groups, groups: ["User"], operations: [read] },

    # Everyone
    { allow: public, operations: [read] }
  ])

我正在通过以下方式切换身份验证方法:

Amplify.configure({
  ...awsconfig,
  aws_appsync_authenticationType: isAuthenticated ? 'AMAZON_COGNITO_USER_POOLS' : 'API_KEY',
});

我可以解决这个问题。 没有为某些自定义类型定义 public 访问权限,因此整个同步失败。在我的方案中取消注释这些并推送更改后,所有数据都可以同步。如果用户未登录,更好的方法是选择性同步以禁用受影响类型的同步。但我还没有尝试过。