Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

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

I've setup an amplify project for react.js, which should allow authorized and unauthorized access to my application. Therefore, I've configured two authentication methods: Cognito User Pools and API key. The first one is my default. My application runs fine, if I sign in via the Cognito User Pool. All data is beeing synchronized via the DataStore. But if I switch to the API Key as unauthenticated user, I only get unauthorized errors under the hood. E.g. for one of my custom types:

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

The API key is sent correctly as "x-api-key".

If I manually query the Graph-API (e.g. via the amplify.js-API in my application or the third party tool GraphiQL), I can get the results. Just the synchronization isn't working as it does for authenticated users.

In my schema, I control the access via @auth. E.g.:

@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] }
  ])

I'm switching the authentication method via:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I could solve the issue. There wasn't a public access defined for some custom types, so the whole synchronization failed. After uncommenting these in my scheme and pushing the changes, all data could be synchronized. A better approach could be selective sync to disable the synchronization for the affected types, if the user isn't signed in. But I haven't tried that yet.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...