简体   繁体   English

放大数据存储订阅错误

[英]Amplify DataStore subscriptionError

I am creating DataStore Amplify API with several entities having owner authorization rule.我正在使用具有所有者授权规则的多个实体创建 DataStore Amplify API。 After starting application I see lots of errors like this:启动应用程序后,我看到很多这样的错误:

31:28.602 DataStore - subscriptionError, Connection failed: {"errors":[{"errorType":"Unauthorized","message":"Not Authorized to access onCreateUnit on type Subscription"}]}

I noticed these errors are related to entities with public auth rule.我注意到这些错误与具有公共授权规则的实体有关。 After I sign in with created user I still see entities from another user, that means owner authorization rule is not working by some reason.在我使用创建的用户登录后,我仍然看到来自另一个用户的实体,这意味着所有者授权规则由于某种原因不起作用。

Steps to reproduce:重现步骤:

amplify init:放大初始化:

Initialize the project with the above configuration? No
? Enter a name for the environment dev
? Choose your default editor: IntelliJ IDEA
? Choose the type of app that you're building javascript
Please tell us about your project
? What javascript framework are you using react-native
? Source Directory Path:  src
? Distribution Directory Path: /
? Build Command:  npm build
? Start Command: npm start
Using default provider  awscloudformation
? Select the authentication method you want to use: AWS profile

amplify add auth:放大添加身份验证:

Using service: Cognito, provided by: awscloudformation
The current configured provider is Amazon Cognito. 
Do you want to use the default authentication and security configuration? Default configuration
Warning: you will not be able to edit these selections. 
How do you want users to be able to sign in? Email
Do you want to configure advanced settings? Yes, I want to make some additional changes.
Warning: you will not be able to edit these selections. 
What attributes are required for signing up? Email

amplify add api:放大添加api:

? Select from one of the below mentioned services: GraphQL
? Here is the GraphQL API that we will create. Select a setting to edit or continue Authorization modes: API key (default, expiration time: 7 days from now)
? Choose the default authorization type for the API Amazon Cognito User Pool
Use a Cognito user pool configured as a part of this project.
? Configure additional auth types? No
? Here is the GraphQL API that we will create. Select a setting to edit or continue Conflict detection (required for DataStore): Disabled
? Enable conflict detection? Yes
? Select the default resolution strategy Auto Merge
? Here is the GraphQL API that we will create. Select a setting to edit or continue Continue
? Choose a schema template: Blank Schema

My model in schema.graphql :我在schema.graphql中的模型:

type Exercise @model @auth(rules: [{allow: owner}]) {
  id: ID!
  name: String
  routines: [Routine] @manyToMany(relationName: "RoutineExercise")
  muscles: [Muscle] @manyToMany(relationName: "ExerciseMuscle")
  Histories: [History] @hasMany(indexName: "byExercise", fields: ["id"])
}

type Routine @model @auth(rules: [{allow: owner}]) {
  id: ID!
  name: String
  planName: String
  Exercises: [Exercise] @manyToMany(relationName: "RoutineExercise")
}

type Unit @model @auth(rules: [{allow: public}]) {
  id: ID!
  name: String
  isActive: Boolean
}

Then I did:然后我做了:

amplify push
amplify codegen models

Here is how I initialize Amplify in App.js:以下是我在 App.js 中初始化 Amplify 的方式:

  useEffect(() => {
    Amplify.configure({
      ...config,
      Analytics: {
        disabled: true,
      },
    });
    const result = checkAuthState();
    store.dispatch(fetchRoutines);
  }, []);

My app authentication looks like this:我的应用程序身份验证如下所示:

  async function signIn() {
    try {
      await Auth.signIn(username, password);
      updateAuthState('loggedIn');
      
    } catch (error) {
      console.log('Error signing in...', error);
    }
  }

  async function checkAuthState() {
    await Auth.currentAuthenticatedUser()
      .then((data) => {
        setUserLoggedIn('loggedIn');
      }).catch((error) => {
        setUserLoggedIn('loggedOut');
      })
  }

  <Provider store={store} >
      <NavigationContainer>
        {isUserLoggedIn === 'initializing' && <Initializing />}
        {isUserLoggedIn === 'loggedIn' && (
          <AppNavigator updateAuthState={updateAuthState} />
        )}
        {isUserLoggedIn === 'loggedOut' && (
          <AuthenticationNavigator updateAuthState={updateAuthState} />
        )}
      </NavigationContainer>
    </Provider>

What am I doing wrong?我究竟做错了什么? I was just following Amplify Docs and it's not working, please help...我只是在关注 Amplify Docs,但它不工作,请帮助...

I had the same error message when following the introductory AWS Amplify tutorial .在遵循介绍性 AWS Amplify 教程时,我收到了相同的错误消息。

Solution for me was to add further config into file index.js:我的解决方案是在文件 index.js 中添加更多配置:

import { AuthModeStrategyType } from 'aws-amplify'

Amplify.configure({
   ...config,
   DataStore: {
     authModeStrategyType: AuthModeStrategyType.MULTI_AUTH
   }
 })

After this change, data updates and authorisation rules all worked as expected with no browser console errors.在此更改之后,数据更新和授权规则都按预期工作,没有浏览器控制台错误。

For more info see this section of AWS lib docs .有关详细信息,请参阅AWS lib 文档的这一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM