forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
116 lines (103 loc) · 3.75 KB
/
Copy pathauth.ts
File metadata and controls
116 lines (103 loc) · 3.75 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { WebStorageStateStore } from "oidc-client-ts";
// Configuration type matching the cognitoAuthConfig structure
type AwsExportsConfig = {
authority?: string;
client_id?: string;
redirect_uri?: string;
post_logout_redirect_uri?: string;
response_type?: string;
scope?: string;
automaticSilentRenew?: boolean;
userStore: WebStorageStateStore | undefined;
};
/**
* Configuration Priority (highest to lowest):
* 1. Environment variables (VITE_COGNITO_*)
* 2. aws-exports.json file
* 3. Default values
*/
// Cache for loaded config
let configCache: AwsExportsConfig | null = null;
let configPromise: Promise<AwsExportsConfig | null> | null = null;
// Load configuration from aws-exports.json at runtime
async function loadAwsConfig(): Promise<AwsExportsConfig | null> {
if (configCache) {
return configCache;
}
if (configPromise) {
return configPromise;
}
configPromise = (async () => {
try {
const response = await fetch("/aws-exports.json");
if (!response.ok) {
throw new Error(`Failed to load aws-exports.json: ${response.status}`);
}
const config = await response.json();
configCache = config;
return config;
} catch (error) {
console.error("Failed to load aws-exports.json:", error);
throw error;
}
})();
return configPromise;
}
// Create auth config factory function that loads config dynamically
export async function createCognitoAuthConfig(): Promise<AwsExportsConfig> {
const awsConfig = await loadAwsConfig();
if (awsConfig === null) {
throw Error("aws-exports.json file not found");
}
// Get environment variables
const userPoolId = import.meta.env.VITE_COGNITO_USER_POOL_ID;
const clientId = import.meta.env.VITE_COGNITO_CLIENT_ID;
const region = import.meta.env.VITE_COGNITO_REGION;
const redirectUri = import.meta.env.VITE_COGNITO_REDIRECT_URI;
const postLogoutRedirectUri = import.meta.env
.VITE_COGNITO_POST_LOGOUT_REDIRECT_URI;
const responseType = import.meta.env.VITE_COGNITO_RESPONSE_TYPE;
const scope = import.meta.env.VITE_COGNITO_SCOPE;
const automaticSilentRenew = import.meta.env
.VITE_COGNITO_AUTOMATIC_SILENT_RENEW;
// Build authority from environment variables if region and userPoolId are provided
const envAuthority =
region && userPoolId
? `https://cognito-idp.${region}.amazonaws.com/${userPoolId}`
: undefined;
return {
authority: envAuthority || awsConfig.authority,
client_id: clientId || awsConfig.client_id,
redirect_uri: redirectUri || awsConfig.redirect_uri,
post_logout_redirect_uri:
postLogoutRedirectUri ||
redirectUri ||
awsConfig.post_logout_redirect_uri,
response_type: responseType || awsConfig.response_type || "code",
scope: scope || awsConfig.scope || "email openid profile",
automaticSilentRenew:
automaticSilentRenew === "false"
? false
: automaticSilentRenew === "true"
? true
: (awsConfig.automaticSilentRenew ?? true),
userStore:
typeof window !== "undefined"
? new WebStorageStateStore({ store: window.localStorage })
: undefined,
};
}
// Synchronous version for backwards compatibility (uses env vars as fallback)
export const cognitoAuthConfig = {
authority: `https://cognito-idp.${import.meta.env.VITE_COGNITO_REGION}.amazonaws.com/${import.meta.env.VITE_COGNITO_USER_POOL_ID}`,
client_id: import.meta.env.VITE_COGNITO_CLIENT_ID,
redirect_uri: import.meta.env.VITE_COGNITO_REDIRECT_URI,
post_logout_redirect_uri: import.meta.env.VITE_COGNITO_REDIRECT_URI,
response_type: "code",
scope: "email openid profile",
automaticSilentRenew: true,
userStore:
typeof window !== "undefined"
? new WebStorageStateStore({ store: window.localStorage })
: undefined,
};