Skip to content

Commit 4d253c1

Browse files
fix(aws-amplify): scope PR to Cognito setAuthConfig on reconfigure only
Remove libraryOptions merge from initSingleton; that belongs in #14815. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent f67c651 commit 4d253c1

4 files changed

Lines changed: 36 additions & 122 deletions

File tree

.changeset/fix-init-singleton-auth-reconfigure.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'aws-amplify': patch
33
---
44

5-
fix(aws-amplify): merge libraryOptions when Auth is overridden and refresh default Cognito auth config on reconfigure.
5+
fix(aws-amplify): refresh default Cognito auth config on DefaultAmplify reconfigure.

packages/aws-amplify/__tests__/initSingleton.integration.test.ts

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,8 @@ const poolBConfig: ResourcesConfig = {
2828
},
2929
};
3030

31-
const storageLibraryOptions = {
32-
Storage: {
33-
S3: {
34-
defaultAccessLevel: 'private' as const,
35-
isObjectLockEnabled: true,
36-
},
37-
},
38-
};
39-
4031
describe('DefaultAmplify.configure integration', () => {
4132
let setAuthConfigSpy: jest.SpyInstance;
42-
let setKeyValueStorageSpy: jest.SpyInstance;
4333

4434
beforeEach(() => {
4535
CoreAmplify.libraryOptions = {};
@@ -48,45 +38,31 @@ describe('DefaultAmplify.configure integration', () => {
4838
cognitoUserPoolsTokenProvider,
4939
'setAuthConfig',
5040
);
51-
setKeyValueStorageSpy = jest.spyOn(
52-
cognitoUserPoolsTokenProvider,
53-
'setKeyValueStorage',
54-
);
5541
});
5642

5743
afterEach(() => {
5844
setAuthConfigSpy.mockRestore();
59-
setKeyValueStorageSpy.mockRestore();
6045
CoreAmplify.libraryOptions = {};
6146
CoreAmplify.resourcesConfig = {};
6247
});
6348

64-
it('keeps Storage and refreshes Cognito auth config on partial reconfigure', () => {
65-
DefaultAmplify.configure(poolAConfig, storageLibraryOptions);
49+
it('syncs Cognito auth config when reconfiguring with partial libraryOptions', () => {
50+
DefaultAmplify.configure(poolAConfig);
6651

67-
expect(CoreAmplify.libraryOptions.Storage).toEqual(
68-
storageLibraryOptions.Storage,
69-
);
70-
expect(CoreAmplify.libraryOptions.Auth?.tokenProvider).toBe(
71-
cognitoUserPoolsTokenProvider,
72-
);
7352
expect(setAuthConfigSpy).toHaveBeenCalledWith(poolAConfig.Auth);
7453

7554
setAuthConfigSpy.mockClear();
7655

7756
DefaultAmplify.configure(poolBConfig, { ssr: false });
7857

79-
expect(CoreAmplify.libraryOptions.Storage).toEqual(
80-
storageLibraryOptions.Storage,
81-
);
58+
expect(setAuthConfigSpy).toHaveBeenCalledWith(poolBConfig.Auth);
8259
expect(CoreAmplify.getConfig().Auth?.Cognito?.userPoolClientId).toBe(
8360
'client-b',
8461
);
85-
expect(setAuthConfigSpy).toHaveBeenCalledWith(poolBConfig.Auth);
8662
});
8763

88-
it('merges prior libraryOptions when libraryOptions.Auth overrides default provider', () => {
89-
DefaultAmplify.configure(poolAConfig, storageLibraryOptions);
64+
it('syncs default Cognito auth config when libraryOptions.Auth uses default provider', () => {
65+
DefaultAmplify.configure(poolAConfig);
9066

9167
setAuthConfigSpy.mockClear();
9268

@@ -98,25 +74,19 @@ describe('DefaultAmplify.configure integration', () => {
9874
},
9975
});
10076

101-
expect(CoreAmplify.libraryOptions.Storage).toEqual(
102-
storageLibraryOptions.Storage,
103-
);
10477
expect(setAuthConfigSpy).toHaveBeenCalledWith(poolBConfig.Auth);
10578
expect(CoreAmplify.getConfig().Auth?.Cognito?.userPoolClientId).toBe(
10679
'client-b',
10780
);
10881
});
10982

11083
it('syncs default Cognito auth config when only resource config is passed', () => {
111-
DefaultAmplify.configure(poolAConfig, storageLibraryOptions);
84+
DefaultAmplify.configure(poolAConfig);
11285

11386
setAuthConfigSpy.mockClear();
11487

11588
DefaultAmplify.configure(poolBConfig);
11689

117-
expect(CoreAmplify.libraryOptions.Storage).toEqual(
118-
storageLibraryOptions.Storage,
119-
);
12090
expect(setAuthConfigSpy).toHaveBeenCalledWith(poolBConfig.Auth);
12191
expect(CoreAmplify.getConfig().Auth?.Cognito?.userPoolId).toBe('pool-b');
12292
});

packages/aws-amplify/__tests__/initSingleton.test.ts

Lines changed: 24 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -246,29 +246,40 @@ describe('initSingleton (DefaultAmplify)', () => {
246246
});
247247

248248
describe('when ResourcesConfig.Auth is defined', () => {
249-
it('should merge with existing libraryOptions when libraryOptions.Auth is defined', () => {
250-
const customTokenProvider = { getTokens: jest.fn() };
251-
const storageLibraryOptions = {
252-
S3: { defaultAccessLevel: 'private' as const },
253-
};
254-
AmplifySingleton.libraryOptions = {
255-
Storage: storageLibraryOptions,
256-
};
249+
it('should sync default Cognito auth config when libraryOptions.Auth uses default provider', () => {
257250
const libraryOptions = {
258-
Auth: { tokenProvider: customTokenProvider },
251+
Auth: {
252+
tokenProvider: cognitoUserPoolsTokenProvider,
253+
credentialsProvider: cognitoCredentialsProvider,
254+
},
259255
};
256+
260257
Amplify.configure(mockResourceConfig, libraryOptions);
261258

259+
expect(
260+
mockCognitoUserPoolsTokenProviderSetAuthConfig,
261+
).toHaveBeenCalledWith(mockResourceConfig.Auth);
262262
expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
263263
mockResourceConfig,
264-
{
265-
Storage: storageLibraryOptions,
266-
Auth: libraryOptions.Auth,
267-
},
264+
libraryOptions,
268265
);
266+
});
267+
268+
it('should not sync Cognito auth config when libraryOptions.Auth uses a custom token provider', () => {
269+
const customTokenProvider = { getTokens: jest.fn() };
270+
const libraryOptions = {
271+
Auth: { tokenProvider: customTokenProvider },
272+
};
273+
274+
Amplify.configure(mockResourceConfig, libraryOptions);
275+
269276
expect(
270277
mockCognitoUserPoolsTokenProviderSetAuthConfig,
271278
).not.toHaveBeenCalled();
279+
expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
280+
mockResourceConfig,
281+
libraryOptions,
282+
);
272283
});
273284

274285
describe('when the singleton libraryOptions have not yet been configured with Auth', () => {
@@ -398,34 +409,6 @@ describe('initSingleton (DefaultAmplify)', () => {
398409
);
399410
});
400411

401-
it('should preserve non-Auth library options when reconfiguring with partial libraryOptions', () => {
402-
const storageLibraryOptions = {
403-
S3: { defaultAccessLevel: 'private' as const },
404-
};
405-
AmplifySingleton.libraryOptions = {
406-
Auth: {
407-
tokenProvider: cognitoUserPoolsTokenProvider,
408-
credentialsProvider: cognitoCredentialsProvider,
409-
},
410-
Storage: storageLibraryOptions,
411-
};
412-
const authLibraryOptions = AmplifySingleton.libraryOptions.Auth;
413-
414-
Amplify.configure(mockResourceConfig, { ssr: true });
415-
416-
expect(
417-
mockCognitoUserPoolsTokenProviderSetAuthConfig,
418-
).toHaveBeenCalledWith(mockResourceConfig.Auth);
419-
expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
420-
mockResourceConfig,
421-
{
422-
Auth: authLibraryOptions,
423-
Storage: storageLibraryOptions,
424-
ssr: true,
425-
},
426-
);
427-
});
428-
429412
it('should sync default Cognito auth config when reconfiguring with resource config only', () => {
430413
Amplify.configure(mockResourceConfig);
431414

@@ -437,33 +420,6 @@ describe('initSingleton (DefaultAmplify)', () => {
437420
);
438421
});
439422

440-
it('should sync default Cognito auth config when libraryOptions.Auth overrides with default provider', () => {
441-
const updatedResourceConfig: ResourcesConfig = {
442-
Auth: {
443-
Cognito: {
444-
userPoolClientId: 'newClientId',
445-
userPoolId: 'newPoolId',
446-
},
447-
},
448-
};
449-
AmplifySingleton.libraryOptions = {
450-
Auth: {
451-
tokenProvider: cognitoUserPoolsTokenProvider,
452-
credentialsProvider: cognitoCredentialsProvider,
453-
},
454-
};
455-
456-
Amplify.configure(updatedResourceConfig, {
457-
Auth: {
458-
tokenProvider: cognitoUserPoolsTokenProvider,
459-
credentialsProvider: cognitoCredentialsProvider,
460-
},
461-
});
462-
463-
expect(
464-
mockCognitoUserPoolsTokenProviderSetAuthConfig,
465-
).toHaveBeenCalledWith(updatedResourceConfig.Auth);
466-
});
467423
});
468424

469425
it('should invoke AmplifySingleton.configure with other provided library options', () => {

packages/aws-amplify/src/initSingleton.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,14 @@ export const DefaultAmplify = {
6666
return;
6767
}
6868

69-
// If Auth options are provided, merge with existing libraryOptions so other categories are preserved.
69+
// If Auth options are provided, always just configure as is.
70+
// Otherwise, we can assume no Auth libraryOptions were provided from here on.
7071
if (libraryOptions?.Auth) {
71-
const mergedLibraryOptions: LibraryOptions = {
72-
...Amplify.libraryOptions,
73-
...libraryOptions,
74-
};
75-
76-
if (usesDefaultCognitoTokenProvider(mergedLibraryOptions.Auth)) {
72+
if (usesDefaultCognitoTokenProvider(libraryOptions.Auth)) {
7773
syncDefaultCognitoAuthConfig(resolvedResourceConfig.Auth);
78-
79-
if (libraryOptions.ssr !== undefined) {
80-
cognitoUserPoolsTokenProvider.setKeyValueStorage(
81-
// TODO: allow configure with a public interface
82-
resolvedKeyValueStorage,
83-
);
84-
}
8574
}
8675

87-
Amplify.configure(resolvedResourceConfig, mergedLibraryOptions);
76+
Amplify.configure(resolvedResourceConfig, libraryOptions);
8877

8978
return;
9079
}
@@ -127,9 +116,8 @@ export const DefaultAmplify = {
127116
}
128117

129118
Amplify.configure(resolvedResourceConfig, {
130-
...Amplify.libraryOptions,
131-
...libraryOptions,
132119
Auth: authLibraryOptions,
120+
...libraryOptions,
133121
});
134122

135123
return;

0 commit comments

Comments
 (0)