Skip to content

Commit 42f6332

Browse files
authored
fix(aws-amplify): cookie store is not used for identity store in SSR (#14326)
* fix(aws-amplify): cookie store is not used for identity store in SSR * chore: resolve comments * chore: resolve comments
1 parent d24a6c0 commit 42f6332

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
import { AmplifyOutputs } from '@aws-amplify/core/internals/utils';
1212

1313
import {
14+
CognitoAWSCredentialsAndIdentityIdProvider,
15+
DefaultIdentityIdStore,
1416
cognitoCredentialsProvider,
1517
cognitoUserPoolsTokenProvider,
1618
} from '../src/auth/cognito';
@@ -23,6 +25,8 @@ jest.mock('../src/auth/cognito', () => ({
2325
setKeyValueStorage: jest.fn(),
2426
},
2527
cognitoCredentialsProvider: jest.fn(),
28+
DefaultIdentityIdStore: jest.fn(),
29+
CognitoAWSCredentialsAndIdentityIdProvider: jest.fn(),
2630
}));
2731

2832
const mockCognitoUserPoolsTokenProviderSetAuthConfig =
@@ -32,6 +36,10 @@ const mockCognitoUserPoolsTokenProviderSetKeyValueStorage =
3236
const mockAmplifySingletonConfigure = AmplifySingleton.configure as jest.Mock;
3337
const mockAmplifySingletonGetConfig = AmplifySingleton.getConfig as jest.Mock;
3438
const MockCookieStorage = CookieStorage as jest.Mock;
39+
const MockDefaultIdentityIdStore = jest.mocked(DefaultIdentityIdStore);
40+
const MockCognitoAWSCredentialsAndIdentityIdProvider = jest.mocked(
41+
CognitoAWSCredentialsAndIdentityIdProvider,
42+
);
3543

3644
const mockResourceConfig: ResourcesConfig = {
3745
Auth: {
@@ -50,8 +58,16 @@ const mockResourceConfig: ResourcesConfig = {
5058

5159
describe('initSingleton (DefaultAmplify)', () => {
5260
const mockCookieStorageInstance = {};
61+
const mockCognitoAWSCredentialsAndIdentityIdProviderInstance = {} as any;
62+
const mockDefaultIdentityIdStoreInstance = {} as any;
5363
beforeAll(() => {
5464
MockCookieStorage.mockImplementation(() => mockCookieStorageInstance);
65+
MockDefaultIdentityIdStore.mockImplementation(
66+
() => mockDefaultIdentityIdStoreInstance,
67+
);
68+
MockCognitoAWSCredentialsAndIdentityIdProvider.mockImplementation(
69+
() => mockCognitoAWSCredentialsAndIdentityIdProviderInstance,
70+
);
5571
});
5672
beforeEach(() => {
5773
mockAmplifySingletonConfigure.mockImplementation((_, libraryOptions) => {
@@ -64,6 +80,8 @@ describe('initSingleton (DefaultAmplify)', () => {
6480

6581
afterEach(() => {
6682
MockCookieStorage.mockClear();
83+
MockCognitoAWSCredentialsAndIdentityIdProvider.mockClear();
84+
MockDefaultIdentityIdStore.mockClear();
6785
mockCognitoUserPoolsTokenProviderSetAuthConfig.mockReset();
6886
mockCognitoUserPoolsTokenProviderSetKeyValueStorage.mockReset();
6987
mockAmplifySingletonConfigure.mockReset();
@@ -252,13 +270,20 @@ describe('initSingleton (DefaultAmplify)', () => {
252270
expect(
253271
mockCognitoUserPoolsTokenProviderSetKeyValueStorage,
254272
).toHaveBeenCalledWith(mockCookieStorageInstance);
273+
expect(MockDefaultIdentityIdStore).toHaveBeenCalledWith(
274+
mockCookieStorageInstance,
275+
);
276+
expect(
277+
MockCognitoAWSCredentialsAndIdentityIdProvider,
278+
).toHaveBeenCalledWith(mockDefaultIdentityIdStoreInstance);
255279
expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
256280
mockResourceConfig,
257281
{
258282
...libraryOptions,
259283
Auth: {
260284
tokenProvider: cognitoUserPoolsTokenProvider,
261-
credentialsProvider: cognitoCredentialsProvider,
285+
credentialsProvider:
286+
mockCognitoAWSCredentialsAndIdentityIdProviderInstance,
262287
},
263288
},
264289
);
@@ -345,7 +370,6 @@ describe('initSingleton (DefaultAmplify)', () => {
345370
expect(
346371
mockCognitoUserPoolsTokenProviderSetAuthConfig,
347372
).not.toHaveBeenCalled();
348-
expect(MockCookieStorage).not.toHaveBeenCalled();
349373
expect(
350374
mockCognitoUserPoolsTokenProviderSetKeyValueStorage,
351375
).not.toHaveBeenCalled();

packages/aws-amplify/src/initSingleton.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
} from '@aws-amplify/core/internals/utils';
1515

1616
import {
17+
CognitoAWSCredentialsAndIdentityIdProvider,
18+
DefaultIdentityIdStore,
1719
cognitoCredentialsProvider,
1820
cognitoUserPoolsTokenProvider,
1921
} from './auth/cognito';
@@ -36,6 +38,15 @@ export const DefaultAmplify = {
3638
libraryOptions?: LibraryOptions,
3739
): void {
3840
const resolvedResourceConfig = parseAmplifyConfig(resourceConfig);
41+
const cookieBasedKeyValueStorage = new CookieStorage({ sameSite: 'lax' });
42+
const resolvedKeyValueStorage = libraryOptions?.ssr
43+
? cookieBasedKeyValueStorage
44+
: defaultStorage;
45+
const resolvedCredentialsProvider = libraryOptions?.ssr
46+
? new CognitoAWSCredentialsAndIdentityIdProvider(
47+
new DefaultIdentityIdStore(cookieBasedKeyValueStorage),
48+
)
49+
: cognitoCredentialsProvider;
3950

4051
// If no Auth config is provided, no special handling will be required, configure as is.
4152
// Otherwise, we can assume an Auth config is provided from here on.
@@ -58,16 +69,14 @@ export const DefaultAmplify = {
5869
cognitoUserPoolsTokenProvider.setAuthConfig(resolvedResourceConfig.Auth);
5970
cognitoUserPoolsTokenProvider.setKeyValueStorage(
6071
// TODO: allow configure with a public interface
61-
libraryOptions?.ssr
62-
? new CookieStorage({ sameSite: 'lax' })
63-
: defaultStorage,
72+
resolvedKeyValueStorage,
6473
);
6574

6675
Amplify.configure(resolvedResourceConfig, {
6776
...libraryOptions,
6877
Auth: {
6978
tokenProvider: cognitoUserPoolsTokenProvider,
70-
credentialsProvider: cognitoCredentialsProvider,
79+
credentialsProvider: resolvedCredentialsProvider,
7180
},
7281
});
7382

@@ -77,18 +86,19 @@ export const DefaultAmplify = {
7786
// At this point, Auth libraryOptions would have been previously configured and no overriding
7887
// Auth options were given, so we should preserve the currently configured Auth libraryOptions.
7988
if (libraryOptions) {
89+
const authLibraryOptions = Amplify.libraryOptions.Auth;
8090
// If ssr is provided through libraryOptions, we should respect the intentional reconfiguration.
8191
if (libraryOptions.ssr !== undefined) {
8292
cognitoUserPoolsTokenProvider.setKeyValueStorage(
8393
// TODO: allow configure with a public interface
84-
libraryOptions.ssr
85-
? new CookieStorage({ sameSite: 'lax' })
86-
: defaultStorage,
94+
resolvedKeyValueStorage,
8795
);
96+
97+
authLibraryOptions.credentialsProvider = resolvedCredentialsProvider;
8898
}
8999

90100
Amplify.configure(resolvedResourceConfig, {
91-
Auth: Amplify.libraryOptions.Auth,
101+
Auth: authLibraryOptions,
92102
...libraryOptions,
93103
});
94104

0 commit comments

Comments
 (0)