Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dehli committed Nov 12, 2024
1 parent 3b96ff7 commit 63df886
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { AmplifyErrorCode } from '@aws-amplify/core/internals/utils';
import { Hub } from 'aws-amplify/utils';
import {
defaultAuthHubHandler,
listenToAuthHub,
} 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;
Expand Down Expand Up @@ -50,7 +52,15 @@ describe('defaultAuthHubHandler', () => {
expect(onSignOut).not.toHaveBeenCalled();
});

it('calls onSignIn callabck on signedIn event if provided', () => {
it('does not call service on tokenRefreh_failure event if NetworkError', () => {
defaultAuthHubHandler(
{ channel: 'auth', payload: { event: 'tokenRefreh_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,
Expand Down
14 changes: 10 additions & 4 deletions packages/ui/src/helpers/authenticator/defaultAuthHubHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AmplifyErrorCode } from '@aws-amplify/core/internals/utils';
import { Hub } from 'aws-amplify/utils';

import { isFunction } from '../../utils';
Expand All @@ -13,7 +14,7 @@ export const defaultAuthHubHandler: AuthMachineHubHandler = (
service,
options
) => {
const { event } = payload;
const { data, event } = payload;
const { send } = service;
const { onSignIn, onSignOut } = options ?? {};

Expand All @@ -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;
}
Expand Down

0 comments on commit 63df886

Please sign in to comment.