Search code examples
reactjsnext.jsaws-amplifyaws-appsyncaws-datastore

Subscription UnauthorizedException warning on "update" of model. How to turn subscription off?


I've recently updated my AWS Amplify front end (NextJS) to remove any subscriptions to a "Card" object that I've created in my schema.graphql file. For some reason I'm still getting warnings in my dev tools console telling me that there's a subscription problem concerning authorisation when performing "update" on the "Card" model. My best guess is that Datastore is still performing some kind of subscription function in the background, but I can't find any information to explain what's going on or how to act on the warning. Could anyone please help explain this one?

My schema.graphql showing the subscriptions should be turned off on Card:

type Card
  @model(subscriptions: null) # Removed subscriptions
{...}

I used to have subscriptions set up in my javascript, but I have removed them too, eg:

// I have deleted all code relating to subscriptions, such as the following:
  const subscription = DataStore.observe(Card).subscribe(() => fetchCards());
  return () => subscription.unsubscribe();

I get this warning message in my dev tools console:

DataStore - subscriptionError Connection failed: {
  "errors":[{
    "errorType":"UnauthorizedException",
    "message":"Permission denied"
   }
]}

Followed by this warning:

react_devtools_backend.js:4026 [WARN] 
39:46.202 DataStore 
{
  recoverySuggestion: 'Ensure app code is up to date, auth directives
  exist and are correct on each model, 
  and that server-side data has not been invalidated by a schema
  change. If the problem persists, search for or create an issue:
  https://github.com/aws-amplify/amplify-js/issues', 
  localModel: null, 
  message: 'Connection failed: 
    {
       "errors": [
         {
           "errorType":"UnauthorizedException", 
           "message":"Permission denied"
          }
        ]
}', 
  model: 'Card', operation: 'Update', 
…}

Solution

  • I had a similar error message when following the introductory AWS Amplify tutorial.

    Solution for me was to add Authentication config into file index.js:

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

    After this change, data updates and authorisation rules all worked as expected with no browser console errors.

    For more info see this section of AWS lib docs.