Skip to content

Commit 12c8289

Browse files
committed
fix(auth): update token provider auth config on reconfigure
When Amplify.configure() is called multiple times (e.g. to switch userPoolClientId for multi-tenant apps), the cognitoUserPoolsTokenProvider was not updated with the new auth config. This caused token refresh to use the stale userPoolClientId, failing with NotAuthorizedException. The fix adds cognitoUserPoolsTokenProvider.setAuthConfig() calls to the two reconfigure code paths that were missing them in initSingleton.ts. Closes #14620
1 parent 24c0a0b commit 12c8289

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'aws-amplify': patch
3+
---
4+
5+
fix(auth): update token provider auth config on reconfigure
6+
7+
When `Amplify.configure()` is called multiple times to switch `userPoolClientId`,
8+
the token provider now receives the updated auth config, ensuring token refresh
9+
uses the correct client ID.

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ describe('initSingleton (DefaultAmplify)', () => {
328328

329329
expect(
330330
mockCognitoUserPoolsTokenProviderSetAuthConfig,
331-
).not.toHaveBeenCalled();
331+
).toHaveBeenCalledWith(mockResourceConfig.Auth);
332332
expect(MockCookieStorage).toHaveBeenCalledWith({ sameSite: 'lax' });
333333
expect(
334334
mockCognitoUserPoolsTokenProviderSetKeyValueStorage,
@@ -348,7 +348,7 @@ describe('initSingleton (DefaultAmplify)', () => {
348348

349349
expect(
350350
mockCognitoUserPoolsTokenProviderSetAuthConfig,
351-
).not.toHaveBeenCalled();
351+
).toHaveBeenCalledWith(mockResourceConfig.Auth);
352352
expect(
353353
mockCognitoUserPoolsTokenProviderSetKeyValueStorage,
354354
).toHaveBeenCalledWith(defaultStorage);
@@ -369,7 +369,7 @@ describe('initSingleton (DefaultAmplify)', () => {
369369

370370
expect(
371371
mockCognitoUserPoolsTokenProviderSetAuthConfig,
372-
).not.toHaveBeenCalled();
372+
).toHaveBeenCalledWith(mockResourceConfig.Auth);
373373
expect(
374374
mockCognitoUserPoolsTokenProviderSetKeyValueStorage,
375375
).not.toHaveBeenCalled();
@@ -382,13 +382,39 @@ describe('initSingleton (DefaultAmplify)', () => {
382382
);
383383
});
384384

385-
it('should just configure without touching libraryOptions', () => {
385+
it('should update auth config and configure without touching libraryOptions', () => {
386386
Amplify.configure(mockResourceConfig);
387387

388+
expect(
389+
mockCognitoUserPoolsTokenProviderSetAuthConfig,
390+
).toHaveBeenCalledWith(mockResourceConfig.Auth);
388391
expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
389392
mockResourceConfig,
390393
);
391394
});
395+
396+
it('should update token provider auth config when userPoolClientId changes on reconfigure', () => {
397+
// First configure
398+
Amplify.configure(mockResourceConfig);
399+
400+
mockCognitoUserPoolsTokenProviderSetAuthConfig.mockClear();
401+
402+
// Reconfigure with different userPoolClientId
403+
const newConfig: ResourcesConfig = {
404+
...mockResourceConfig,
405+
Auth: {
406+
Cognito: {
407+
userPoolClientId: 'newUserPoolClientId',
408+
userPoolId: 'userPoolId',
409+
},
410+
},
411+
};
412+
Amplify.configure(newConfig);
413+
414+
expect(
415+
mockCognitoUserPoolsTokenProviderSetAuthConfig,
416+
).toHaveBeenCalledWith(newConfig.Auth);
417+
});
392418
});
393419

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

packages/aws-amplify/src/initSingleton.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ export const DefaultAmplify = {
9797
authLibraryOptions.credentialsProvider = resolvedCredentialsProvider;
9898
}
9999

100+
cognitoUserPoolsTokenProvider.setAuthConfig(resolvedResourceConfig.Auth);
101+
100102
Amplify.configure(resolvedResourceConfig, {
101103
Auth: authLibraryOptions,
102104
...libraryOptions,
@@ -107,6 +109,7 @@ export const DefaultAmplify = {
107109

108110
// Finally, if there were no libraryOptions given at all, we should simply not touch the currently
109111
// configured libraryOptions.
112+
cognitoUserPoolsTokenProvider.setAuthConfig(resolvedResourceConfig.Auth);
110113
Amplify.configure(resolvedResourceConfig);
111114
},
112115
/**

0 commit comments

Comments
 (0)