-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathcreateRunWithAmplifyServerContext.ts
More file actions
92 lines (81 loc) · 3.17 KB
/
Copy pathcreateRunWithAmplifyServerContext.ts
File metadata and controls
92 lines (81 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { ResourcesConfig, createAmplifyContext } from 'aws-amplify';
import { sharedInMemoryStorage } from 'aws-amplify/utils';
import { KeyValueStorageMethodValidator } from 'aws-amplify/adapter-core/internals';
import {
createAWSCredentialsAndIdentityIdProvider,
createKeyValueStorageFromCookieStorageAdapter,
createUserPoolsTokenProvider,
} from 'aws-amplify/adapter-core';
import { NextServer } from '../types';
import {
DEFAULT_SERVER_SIDE_AUTH_SET_COOKIE_OPTIONS,
ENFORCED_SERVER_SIDE_AUTH_SET_COOKIE_OPTIONS,
} from '../auth/constant';
import { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext';
export const createRunWithAmplifyServerContext = ({
config: resourcesConfig,
tokenValidator,
globalSettings,
}: {
config: ResourcesConfig;
tokenValidator?: KeyValueStorageMethodValidator;
globalSettings: NextServer.GlobalSettings;
}) => {
const isServerSideAuthEnabled = globalSettings.isServerSideAuthEnabled();
const isSSLOrigin = globalSettings.isSSLOrigin();
const setCookieOptions = globalSettings.getRuntimeOptions().cookies ?? {};
const mergedSetCookieOptions = {
// default options when not specified
...(isServerSideAuthEnabled && DEFAULT_SERVER_SIDE_AUTH_SET_COOKIE_OPTIONS),
// user-specified options
...setCookieOptions,
// enforced options when server-side auth is enabled
...(isServerSideAuthEnabled && {
...ENFORCED_SERVER_SIDE_AUTH_SET_COOKIE_OPTIONS,
secure: isSSLOrigin,
}),
// only support root path
path: '/',
};
const runWithAmplifyServerContext: NextServer.RunOperationWithContext =
async ({ nextServerContext, operation }) => {
// When the Auth config is presented, attempt to create a Amplify server
// context with token and credentials provider.
if (resourcesConfig.Auth) {
const keyValueStorage =
// When `null` is passed as the value of `nextServerContext`, opt-in
// unauthenticated role (primarily for static rendering). It's
// safe to use the singleton `MemoryKeyValueStorage` here, as the
// static rendering uses the same unauthenticated role cross-sever.
nextServerContext === null
? sharedInMemoryStorage
: createKeyValueStorageFromCookieStorageAdapter(
await createCookieStorageAdapterFromNextServerContext(
nextServerContext,
isServerSideAuthEnabled,
),
tokenValidator,
mergedSetCookieOptions,
);
const credentialsProvider = createAWSCredentialsAndIdentityIdProvider(
resourcesConfig.Auth,
keyValueStorage,
);
const tokenProvider = createUserPoolsTokenProvider(
resourcesConfig.Auth,
keyValueStorage,
);
const amplifyContext = createAmplifyContext(resourcesConfig, {
Auth: { credentialsProvider, tokenProvider },
});
return operation(amplifyContext);
}
// Otherwise it may be the case that auth is not used, e.g. API key.
// Omitting the `Auth` in the second parameter.
const amplifyContext = createAmplifyContext(resourcesConfig, {});
return operation(amplifyContext);
};
return runWithAmplifyServerContext;
};