diff --git a/packages/ui/src/helpers/authenticator/__tests__/defaultAuthHubHandler.test.ts b/packages/ui/src/helpers/authenticator/__tests__/defaultAuthHubHandler.test.ts index 4307834a571..39a7061f62f 100644 --- a/packages/ui/src/helpers/authenticator/__tests__/defaultAuthHubHandler.test.ts +++ b/packages/ui/src/helpers/authenticator/__tests__/defaultAuthHubHandler.test.ts @@ -1,3 +1,4 @@ +import { AmplifyErrorCode } from '@aws-amplify/core/internals/utils'; import { Hub } from 'aws-amplify/utils'; import { defaultAuthHubHandler, @@ -5,6 +6,7 @@ import { } from '../defaultAuthHubHandler'; import { AuthInterpreter } from '../types'; +const MockNetworkError = { name: AmplifyErrorCode.NetworkError }; const onSignIn = jest.fn(); const onSignOut = jest.fn(); const service = { send: jest.fn() } as unknown as AuthInterpreter; @@ -41,16 +43,30 @@ describe('defaultAuthHubHandler', () => { expect(onSignOut).toHaveBeenCalledTimes(1); }); - it('does not call onSignOut callback on tokenRefreh_failure event if provided', () => { + it('does not call onSignOut callback on tokenRefresh_failure event if provided', () => { defaultAuthHubHandler( - { channel: 'auth', payload: { event: 'tokenRefreh_failure' } }, + { channel: 'auth', payload: { event: 'tokenRefresh_failure' } }, service, { onSignOut } ); expect(onSignOut).not.toHaveBeenCalled(); }); - it('calls onSignIn callabck on signedIn event if provided', () => { + it('does not call service on tokenRefresh_failure event if NetworkError', () => { + defaultAuthHubHandler( + { + channel: 'auth', + payload: { + event: 'tokenRefresh_failure', + data: { error: MockNetworkError }, + }, + }, + service + ); + expect(service.send).not.toHaveBeenCalled(); + }); + + it('calls onSignIn callback on signedIn event if provided', () => { defaultAuthHubHandler( { channel: 'auth', payload: { event: 'signedIn' } }, service, diff --git a/packages/ui/src/helpers/authenticator/defaultAuthHubHandler.ts b/packages/ui/src/helpers/authenticator/defaultAuthHubHandler.ts index 94066919bf5..b6843535f3c 100644 --- a/packages/ui/src/helpers/authenticator/defaultAuthHubHandler.ts +++ b/packages/ui/src/helpers/authenticator/defaultAuthHubHandler.ts @@ -1,3 +1,4 @@ +import { AmplifyErrorCode } from '@aws-amplify/core/internals/utils'; import { Hub } from 'aws-amplify/utils'; import { isFunction } from '../../utils'; @@ -13,7 +14,7 @@ export const defaultAuthHubHandler: AuthMachineHubHandler = ( service, options ) => { - const { event } = payload; + const { data, event } = payload; const { send } = service; const { onSignIn, onSignOut } = options ?? {}; @@ -28,14 +29,19 @@ export const defaultAuthHubHandler: AuthMachineHubHandler = ( send('SIGN_IN_WITH_REDIRECT'); break; } - case 'signedOut': - case 'tokenRefresh_failure': { - if (event === 'signedOut' && isFunction(onSignOut)) { + case 'signedOut': { + if (isFunction(onSignOut)) { onSignOut(); } send('SIGN_OUT'); break; } + case 'tokenRefresh_failure': { + if (data?.error?.name !== AmplifyErrorCode.NetworkError) { + send('SIGN_OUT'); + } + break; + } default: { break; }