Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(aws-amplify): preserve default auth providers with repetitive calls of configure #12478

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/aws-amplify/__tests__/initSingleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down Expand Up @@ -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: {
Expand Down
6 changes: 5 additions & 1 deletion packages/aws-amplify/src/initSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder this should be private?

) {
CognitoUserPoolsTokenProvider.setAuthConfig(resolvedResourceConfig.Auth);

const libraryOptionsWithDefaultAuthProviders: LibraryOptions = {
Expand Down
Loading