amplify-js: Authenticator signs users out on network error when attempting to refresh the user tokens with valid refresh token

Describe the bug This is probably intended behavior I guess, but I’m opening this as a bug since it causes problems for my use case and I’m not sure whether the current behavior is ideal for other people either. For my scenario, (react app running in a chrome extension) I set the refresh token to a very high value because having to log back in is annoying (chrome doesn’t save passwords for pages running in chrome extensions). This works fine, except that the access token and id token can only be set to last 1 day max. So what I’m hitting is that upon opening a laptop or coming out of sleep mode, if you run the chrome extension fast enough the network isn’t working yet and amplify attempts to refresh the access\id token because it’s expired, fails for completely temporary reasons, and then signs the user out. While this is particularly annoying due to some specifics of my use case it seems like this could affect any app (or perhaps just any pwa?)

To Reproduce Steps to reproduce the behavior: Set Cognito Access Token and Id Token refresh to 5 minutes. Set Cognito Refresh Token value to a high value (days) (Open Chrome Browser in Windows) Log in. Disconnect from all internet (disable wifi\ethernet). Wait 6 minutes Refresh site. Logger shows: ConsoleLogger.ts:99 [DEBUG] 59:08.496 AuthClass - Failed to get the user session {code: “NetworkError”, name: “Error”, message: “Network error”} ConsoleLogger.ts:99 [DEBUG] 59:08.497 AuthClass - The user is not authenticated by the error {code: “NetworkError”, name: “Error”, message: “Network error”} ConsoleLogger.ts:99 [DEBUG] 59:08.498 AuthClass - user sign out CognitoUser {username: “sampleUser”, pool: CognitoUserPool, Session: null, client: Client, signInUserSession: null, …} ConsoleLogger.ts:99 [DEBUG] 59:08.499 Hub - Dispatching to auth with {event: “signOut”, data: null, message: “A user has been signed out”}

(I can provide a sample chrome extension that can be loaded as an unpacked extension that reproes if necessary)

Expected behavior A few options that would work fine for me:

  1. Keeping the user in the signedIn state. After all, the refresh token is still valid so the access token will get refreshed as soon as the network comes back and the access token is only needed for network requests.
  2. Some kind of new auth state since loading isn’t ideal, and signedIn isn’t really accurate. AuthState.Offline? Adding enum values would be a breaking change though so I assume that one isn’t happening.
  3. Some way to pass in a handler for errors so that it’s possible to catch the network error and override (or not perform) the sign-out behavior.

Code Snippet

const App = () => (
    <div>
        <Authenticator  hideDefault={true}>
            <AppPane /> 
        </Authenticator>
    </div>
);

Is this a production issue? This is an issue in a hobby project that makes my parents complain to me that things aren’t working correctly for them 😃 . But I don’t think anyone could ship an application using authenticator in a chrome extension due to this issue (admittedly a strange scenario). It hits surprisingly frequently even with a navigator.onLine check being confirmed before loading Authenticator (not too surprising since navigator.onLine is mostly useless in general).

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (6 by maintainers)

Most upvoted comments

Hi @stephenreading It might have been a bug like u reported. I just heard from the team it is now fixed.

Side note: CLI version matters too. Something the setup might be from CLI itself that might cause an issue. I would try again with latest amplify js version and CLI version to see if you can reproduce this now. I tried without chrome extension in a react app I couldn’t reproduce it.

@chrisbonifacio This sounds like we should transfer this issue to aws-amplify/amplify-js because the unexpected behavior is that the user is being signed out & cleared because of a Network error, not that the credentials are actually invalid.