Skip to content

Commit

Permalink
fix(auth): wrongly clear tokens on network error when refresh tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed Oct 25, 2024
1 parent dec95c6 commit 9b9c7dd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import {
AmplifyError,
AmplifyErrorCode,
} from '@aws-amplify/core/internals/utils';

import { tokenOrchestrator } from '../../../../src/providers/cognito/tokenProvider';
import { CognitoAuthTokens } from '../../../../src/providers/cognito/tokenProvider/types';
import { oAuthStore } from '../../../../src/providers/cognito/utils/oauth/oAuthStore';

jest.mock('@aws-amplify/core/internals/utils');
jest.mock('@aws-amplify/core', () => ({
...jest.requireActual('@aws-amplify/core'),
Hub: {
Expand Down Expand Up @@ -90,4 +94,20 @@ describe('tokenOrchestrator', () => {
expect(newTokens?.signInDetails).toEqual(testSignInDetails);
});
});

describe('handleErrors method', () => {
it('does not call clearTokens() if the error is a network error thrown from fetch handler', () => {
const clearTokensSpy = jest.spyOn(tokenOrchestrator, 'clearTokens');
const error = new AmplifyError({
name: AmplifyErrorCode.NetworkError,
message: 'Network Error',
});

expect(() => {
(tokenOrchestrator as any).handleErrors(error);
}).toThrow(error);

expect(clearTokensSpy).not.toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@aws-amplify/core';
import {
AMPLIFY_SYMBOL,
AmplifyErrorCode,
assertTokenProviderConfig,
isBrowser,
isTokenExpired,
Expand Down Expand Up @@ -169,7 +170,7 @@ export class TokenOrchestrator implements AuthTokenOrchestrator {

private handleErrors(err: unknown) {
assertServiceError(err);
if (err.message !== 'Network error') {
if (err.name !== AmplifyErrorCode.NetworkError) {
// TODO(v6): Check errors on client
this.clearTokens();
}
Expand Down

0 comments on commit 9b9c7dd

Please sign in to comment.