Reputation: 2352
I have a React application that currently uses Amplify GraphQL APIs. I have set those APIs up so that they require using Cognito user pools for authorization. For example the following code is successfully retrieving a list of values from my Projects collection in DynamoDB
const apiData = await API.graphql({ query: listProjects, authMode: "AMAZON_COGNITO_USER_POOLS" });
Recently I have been trying to setup a subscription so that I can receive real time updates from AppSync when new entries are added to my Projects collection. This is the code that I am trying to get to work:
const subscription = API.graphql({query: subscriptions.onUpdateProject, authMode: "AMAZON_COGNITO_USER_POOLS"}
).subscribe({
next: ({ provider, value }) => console.log({ provider, value }),
error: error => console.warn(error)
});
Unfortunately, this code is failing when executed, it is causing this error to be raised:
"Connection failed: {\"errors\":[{\"errorType\":\"Unauthorized\",\"message\":\"Not Authorized to access onUpdateProject on type Project\"}]}"
I am not quite sure how to troubleshoot this or resolve it, do subscriptions require a special configuration with respect to authorization? Are Cognito user pools supported with subscriptions? Any help or guidance would be greatly appreciated.
Upvotes: 1
Views: 882
Reputation: 51
This worked for me - adding the AuthModeStrategyType.MULTI_AUTH
to the config:
import { Amplify, AuthModeStrategyType } from 'aws-amplify';
import awsconfig from './aws-exports';
Amplify.configure({
...awsconfig,
DataStore: {
authModeStrategyType: AuthModeStrategyType.MULTI_AUTH
}
})
Upvotes: 0
Reputation: 2352
I was able to resolve this by adding the a variable to specify the owner to my subscription as shown below. I found this in another issue (https://github.com/aws-amplify/amplify-category-api/issues/456).
const updateProjectSubscription = API.graphql({query: subscriptions.onUpdateProject, authMode: 'AMAZON_COGNITO_USER_POOLS', variables: {
owner: Auth.user.attributes.sub
}}
Upvotes: 2