From 24b861e7feea7baa12ec8723c14c9ec368932dee Mon Sep 17 00:00:00 2001 From: Hui Zhao Date: Tue, 31 Oct 2023 09:50:06 -0700 Subject: [PATCH] fix(aws-amplify): preserve default auth providers with repetitive calls of configure --- .../__tests__/initSingleton.test.ts | 40 +++++++++++++++++++ packages/aws-amplify/src/initSingleton.ts | 6 ++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/packages/aws-amplify/__tests__/initSingleton.test.ts b/packages/aws-amplify/__tests__/initSingleton.test.ts index 0945cc6c1ae..3bdad8a9d86 100644 --- a/packages/aws-amplify/__tests__/initSingleton.test.ts +++ b/packages/aws-amplify/__tests__/initSingleton.test.ts @@ -53,6 +53,13 @@ describe('initSingleton (DefaultAmplify)', () => { mockCognitoUserPoolsTokenProviderSetKeyValueStorage.mockReset(); mockAmplifySingletonConfigure.mockReset(); mockAmplifySingletonGetConfig.mockReset(); + + mockAmplifySingletonConfigure.mockImplementation((_, libraryOptions) => { + AmplifySingleton.libraryOptions = + libraryOptions ?? AmplifySingleton.libraryOptions; + }); + // reset to its initial state + AmplifySingleton.libraryOptions = {}; }); describe('DefaultAmplify.configure()', () => { @@ -137,6 +144,39 @@ describe('initSingleton (DefaultAmplify)', () => { ); }); + it('should preserve the default auth providers when Amplify.configure is called again without custom auth provider', () => { + mockAmplifySingletonGetConfig.mockReturnValueOnce(mockResourceConfig); + + Amplify.configure(mockResourceConfig); + const defaultAuthProvidersHaveBeenConfigured = + AmplifySingleton.libraryOptions; + + Amplify.configure({ + ...Amplify.getConfig(), + Analytics: { + Kinesis: { + region: 'us-west-2', + }, + }, + }); + expect(AmplifySingleton.libraryOptions).toStrictEqual( + defaultAuthProvidersHaveBeenConfigured + ); + + Amplify.configure({ + ...Amplify.getConfig(), + Analytics: { + Personalize: { + trackingId: 'f1b2d240-f7e7-416a-af88-759d7e258994', + region: 'us-west-2', + }, + }, + }); + expect(AmplifySingleton.libraryOptions).toStrictEqual( + defaultAuthProvidersHaveBeenConfigured + ); + }); + it('should invoke AmplifySingleton.configure with other provided library options', () => { const libraryOptionsWithStorage = { Storage: { diff --git a/packages/aws-amplify/src/initSingleton.ts b/packages/aws-amplify/src/initSingleton.ts index 289eef5587e..2ec413012c9 100644 --- a/packages/aws-amplify/src/initSingleton.ts +++ b/packages/aws-amplify/src/initSingleton.ts @@ -31,7 +31,11 @@ export const DefaultAmplify = { // When Auth config is provided but no custom Auth provider defined // use the default Auth Providers - if (resolvedResourceConfig.Auth && !libraryOptions?.Auth) { + if ( + resolvedResourceConfig.Auth && + !libraryOptions?.Auth && + !Amplify.libraryOptions.Auth + ) { CognitoUserPoolsTokenProvider.setAuthConfig(resolvedResourceConfig.Auth); const libraryOptionsWithDefaultAuthProviders: LibraryOptions = {