amplify-ui: fetchAuthSession triggering logout when offline

Before opening, please confirm:

Fix can be found here: https://github.com/aws-amplify/amplify-ui/pull/6061

JavaScript Framework

React

Amplify APIs

Authentication

Amplify Version

v6

Amplify Categories

auth

Backend

CDK

Environment information

# Put output below this line


Describe the bug

If you call Auth.fetchAuthSession({ forceRefresh: true }) while offline, amplify will trigger the logout flow.

Expected behavior

I’d expect an error, but I wouldn’t expect to be logged out.

Reproduction steps

  1. Install latest version of AWS amplify with auth.
  2. Sign in.
  3. Go offline.
  4. Call Auth.fetchAuthSession({ forceRefresh: true }) (I did this by adding the function to the window).

Code Snippet

// Put your code below this line.
window.TRIGGER_ERROR = () => Auth.fetchAuthSession({ forceRefresh: true });

Log output

// Put your logs below this line
{"channel":"auth","payload":{"event":"tokenRefresh_failure","data":{"error":{"name":"NetworkError","underlyingError":{}}}},"source":"Auth","patternInfo":[]}

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

Related to https://github.com/aws-amplify/amplify-js/issues/13596 & https://github.com/aws-amplify/amplify-js/issues/13993

The below chunk of code is NOT responsible for clearing the tokens. I confirmed that the signout workflow is triggered further downstream.

https://github.com/aws-amplify/amplify-js/blob/0f5091780046b9556b98300c29fb970a0358bd70/packages/auth/src/providers/cognito/tokenProvider/TokenOrchestrator.ts#L173-L176

If I return null (and prevent calling the subsequent Hub.dispatch) I am not logged out. Not sure what implications that will have so I didn’t submit a PR.

        if (err.name !== AmplifyErrorCode.NetworkError) {
            // TODO(v6): Check errors on client
            this.clearTokens();
        } else {
            return null; // <-- Adding this "fixes" the issue
        }
        Hub.dispatch('auth', {
            event: 'tokenRefresh_failure',
            data: { error: err },
        }, 'Auth', AMPLIFY_SYMBOL);

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 19 (17 by maintainers)

Commits related to this issue

Most upvoted comments

The fix has been deployed with @aws-amplify/ui@6.8.2. I’m closing out this issue as the release should resolve it, but if you’re running into further problems, please reply back and we can reopen and follow-up as needed. Thank you once again for your contributions!

@didemkkaslan Ideally you want log customer out when there is error except for Network error.

No custom storage. I’ll work on putting together a minimal reproducing example though 👍

@dehli @didemkkaslan Can you confirm which version are you both on? There was a retry fix just recently, while i see the issue on 6.6.4 (I used this version since that was what @didemkkaslan issue mentioned and on another issue as well) when i try the most recent version 6.8.0 i dont see the logout behavior anymore. Can one of you confirm ?

@dehli Thanks for taking a look. I can confirm i see the same behavior as well. I will look closer into this today and report back.

Having the same issue, I see tokenRefresh_failure event being fired and tokens are removed from cookie storage