React Native 放大数据存储订阅错误

wj8zmpe1  于 2022-12-24  发布在  React
关注(0)|答案(1)|浏览(138)

我正在创建DataStore Amplify API,其中包含几个具有所有者授权规则的实体。启动应用程序后,我看到很多这样的错误:

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

我注意到这些错误与具有公共授权规则的实体有关。在我使用创建的用户登录后,我仍然看到来自另一个用户的实体,这意味着所有者授权规则由于某种原因不起作用。
重现步骤:

    • 放大初始化:**
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
    • 放大添加授权:**
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
    • 扩增添加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

我在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
}

然后我就说:

amplify push
amplify codegen models

下面是我如何在App.js中初始化Amplify:

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

我的应用身份验证如下所示:

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>

我做错了什么?我只是按照放大文档,它不工作,请帮助...

ru9i0ody

ru9i0ody1#

当我跟随introductory AWS Amplify tutorial时,我有同样的错误信息。
对我来说,解决方案是添加更多的配置到文件index.js:

import { AuthModeStrategyType } from 'aws-amplify'

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

此更改后,数据更新和授权规则均按预期工作,没有浏览器控制台错误。
有关详细信息,请参阅AWS库文档的此部分。

相关问题