UNPKG

3.26 kBJavaScriptView Raw
1import { Amplify, CookieStorage, defaultStorage } from '@aws-amplify/core';
2import { parseAWSExports } from '@aws-amplify/core/internals/utils';
3import { cognitoUserPoolsTokenProvider, cognitoCredentialsProvider } from '@aws-amplify/auth/cognito';
4
5// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6// SPDX-License-Identifier: Apache-2.0
7const DefaultAmplify = {
8 configure(resourceConfig, libraryOptions) {
9 let resolvedResourceConfig;
10 if (Object.keys(resourceConfig).some(key => key.startsWith('aws_'))) {
11 resolvedResourceConfig = parseAWSExports(resourceConfig);
12 }
13 else {
14 resolvedResourceConfig = resourceConfig;
15 }
16 // If no Auth config is provided, no special handling will be required, configure as is.
17 // Otherwise, we can assume an Auth config is provided from here on.
18 if (!resolvedResourceConfig.Auth) {
19 return Amplify.configure(resolvedResourceConfig, libraryOptions);
20 }
21 // If Auth options are provided, always just configure as is.
22 // Otherwise, we can assume no Auth libraryOptions were provided from here on.
23 if (libraryOptions?.Auth) {
24 return Amplify.configure(resolvedResourceConfig, libraryOptions);
25 }
26 // If no Auth libraryOptions were previously configured, then always add default providers.
27 if (!Amplify.libraryOptions.Auth) {
28 cognitoUserPoolsTokenProvider.setAuthConfig(resolvedResourceConfig.Auth);
29 cognitoUserPoolsTokenProvider.setKeyValueStorage(
30 // TODO: allow configure with a public interface
31 libraryOptions?.ssr
32 ? new CookieStorage({ sameSite: 'lax' })
33 : defaultStorage);
34 return Amplify.configure(resolvedResourceConfig, {
35 ...libraryOptions,
36 Auth: {
37 tokenProvider: cognitoUserPoolsTokenProvider,
38 credentialsProvider: cognitoCredentialsProvider,
39 },
40 });
41 }
42 // At this point, Auth libraryOptions would have been previously configured and no overriding
43 // Auth options were given, so we should preserve the currently configured Auth libraryOptions.
44 if (libraryOptions) {
45 // If ssr is provided through libraryOptions, we should respect the intentional reconfiguration.
46 if (libraryOptions.ssr !== undefined) {
47 cognitoUserPoolsTokenProvider.setKeyValueStorage(
48 // TODO: allow configure with a public interface
49 libraryOptions.ssr
50 ? new CookieStorage({ sameSite: 'lax' })
51 : defaultStorage);
52 }
53 return Amplify.configure(resolvedResourceConfig, {
54 Auth: Amplify.libraryOptions.Auth,
55 ...libraryOptions,
56 });
57 }
58 // Finally, if there were no libraryOptions given at all, we should simply not touch the currently
59 // configured libraryOptions.
60 Amplify.configure(resolvedResourceConfig);
61 },
62 getConfig() {
63 return Amplify.getConfig();
64 },
65};
66
67export { DefaultAmplify };
68//# sourceMappingURL=initSingleton.mjs.map