Amplify DataStore subscriptionError

Viewed 338

I am creating DataStore Amplify API with several entities having owner authorization rule. 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:

? 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:

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:

  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...

0 Answers
Related