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
54import { Amplify } from 'aws-amplify';
import * as Auth from 'aws-amplify/auth';
const generateHeaders = async (apiKeyRequired: boolean) => {
const session = await Auth.fetchAuthSession();
const headers: Record<string, string> = {
Authorization: `Bearer ${session.tokens?.idToken?.toString()}`,
};
if (apiKeyRequired) {
headers['X-Api-Key'] = '1';
}
return headers;
};
Amplify.configure(
{
Auth: {
Cognito: {
userPoolId: import.meta.env.VITE_USERPOOLID,
userPoolClientId: import.meta.env.VITE_USERPOOLWEBCLIENTID,
},
},
API: {
GraphQL: {
endpoint: `${import.meta.env.VITE_API_URL}/graphql`,
defaultAuthMode: 'none',
},
REST: {
public: {
endpoint: `${import.meta.env.VITE_API_URL}`,
},
auth: {
endpoint: `${import.meta.env.VITE_API_URL}`,
},
},
},
},
{
API: {
GraphQL: {
headers: () => generateHeaders(false),
},
REST: {
headers: async ({ apiName }) => {
const apiKeyRequired = apiName !== 'auth';
return generateHeaders(apiKeyRequired);
},
},
},
},
);