放大数据存储订阅错误



我正在创建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>

我做错了什么?我只是在关注Amplify Docs,它不起作用,请帮忙。。。

在学习AWS Amplify入门教程时,我收到了同样的错误消息。

对我来说,解决方案是在文件index.js:中添加进一步的配置

import { AuthModeStrategyType } from 'aws-amplify'
Amplify.configure({
...config,
DataStore: {
authModeStrategyType: AuthModeStrategyType.MULTI_AUTH
}
})

在这次更改之后,数据更新和授权规则都按预期运行,没有出现浏览器控制台错误。

有关更多信息,请参阅AWS lib文档的这一部分。

最新更新