fix(api-graphql): trigger WebSocket reconnection on auth errors #14569
+43
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes the critical issue where DataStore subscriptions silently fail after authentication tokens expire, causing users to miss real-time updates.
Resolves #12954
Problem
After extended idle periods or device sleep/wake cycles (~12-24 hours):
Root Cause
The WebSocket provider doesn't detect authentication errors in subscription messages to trigger reconnection with fresh tokens. When a subscription receives an auth error (UnauthorizedException, Token expired, etc.), it simply errors out without forcing the WebSocket to reconnect.
Solution
Enhanced the WebSocket provider's error handling to:
UnauthorizedException
/Unauthorized
(error types)Token expired
/NotAuthorizedException
(error messages)401
/403
The DataStore's disconnectionHandler already has auth error detection from a previous attempt, so this completes the fix by ensuring the WebSocket layer properly handles token expiration.
Testing
Reproduced and verified fix using steps from issue:
@auth(rules: [{allow: owner}])
)Code Changes
packages/api-graphql/src/Providers/AWSWebSocketProvider/index.ts
Impact
Critical fix for production applications, especially:
As reported by @alex-breen: "leading to data loss on an add request (amplify js doesn't throw an error, but sync fails to write, but app doesn't know this, so data is lost)"
Related Issues