Skip to content
Open
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
9 changes: 9 additions & 0 deletions .changeset/fix-stale-auth-config-reconfigure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'aws-amplify': patch
---

fix(auth): update token provider auth config on reconfigure

When `Amplify.configure()` is called multiple times to switch `userPoolClientId`,
the token provider now receives the updated auth config, ensuring token refresh
uses the correct client ID.
34 changes: 30 additions & 4 deletions packages/aws-amplify/__tests__/initSingleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ describe('initSingleton (DefaultAmplify)', () => {

expect(
mockCognitoUserPoolsTokenProviderSetAuthConfig,
).not.toHaveBeenCalled();
).toHaveBeenCalledWith(mockResourceConfig.Auth);
expect(MockCookieStorage).toHaveBeenCalledWith({ sameSite: 'lax' });
expect(
mockCognitoUserPoolsTokenProviderSetKeyValueStorage,
Expand All @@ -348,7 +348,7 @@ describe('initSingleton (DefaultAmplify)', () => {

expect(
mockCognitoUserPoolsTokenProviderSetAuthConfig,
).not.toHaveBeenCalled();
).toHaveBeenCalledWith(mockResourceConfig.Auth);
expect(
mockCognitoUserPoolsTokenProviderSetKeyValueStorage,
).toHaveBeenCalledWith(defaultStorage);
Expand All @@ -369,7 +369,7 @@ describe('initSingleton (DefaultAmplify)', () => {

expect(
mockCognitoUserPoolsTokenProviderSetAuthConfig,
).not.toHaveBeenCalled();
).toHaveBeenCalledWith(mockResourceConfig.Auth);
expect(
mockCognitoUserPoolsTokenProviderSetKeyValueStorage,
).not.toHaveBeenCalled();
Expand All @@ -382,13 +382,39 @@ describe('initSingleton (DefaultAmplify)', () => {
);
});

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

expect(
mockCognitoUserPoolsTokenProviderSetAuthConfig,
).toHaveBeenCalledWith(mockResourceConfig.Auth);
expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
mockResourceConfig,
);
});

it('should update token provider auth config when userPoolClientId changes on reconfigure', () => {
// First configure
Amplify.configure(mockResourceConfig);

mockCognitoUserPoolsTokenProviderSetAuthConfig.mockClear();

// Reconfigure with different userPoolClientId
const newConfig: ResourcesConfig = {
...mockResourceConfig,
Auth: {
Cognito: {
userPoolClientId: 'newUserPoolClientId',
userPoolId: 'userPoolId',
},
},
};
Amplify.configure(newConfig);

expect(
mockCognitoUserPoolsTokenProviderSetAuthConfig,
).toHaveBeenCalledWith(newConfig.Auth);
});
});

it('should invoke AmplifySingleton.configure with other provided library options', () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/aws-amplify/src/initSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export const DefaultAmplify = {
authLibraryOptions.credentialsProvider = resolvedCredentialsProvider;
}

cognitoUserPoolsTokenProvider.setAuthConfig(resolvedResourceConfig.Auth);

Amplify.configure(resolvedResourceConfig, {
Auth: authLibraryOptions,
...libraryOptions,
Expand All @@ -107,6 +109,7 @@ export const DefaultAmplify = {

// Finally, if there were no libraryOptions given at all, we should simply not touch the currently
// configured libraryOptions.
cognitoUserPoolsTokenProvider.setAuthConfig(resolvedResourceConfig.Auth);
Amplify.configure(resolvedResourceConfig);
},
/**
Expand Down