Skip to content

Commit

Permalink
refactored layout common file
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavanakarwade committed Dec 17, 2024
1 parent 6fc6e4d commit d084df7
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/app/LayoutCommon.astro
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,27 @@ const { class: clazz, metaData } = Astro.props;
const initData: any = {};
const allEnvKeys = [...Object.keys(process.env), ...Object.keys(import.meta.env)];
// Step 1: List of keys that should NOT be exposed to the frontend
const excludeKeys = ['PUBLIC_CRYPTO_PRIVATE_KEY', 'PUBLIC_KEYCLOAK_MANAGEMENT_CLIENT_ID', 'PUBLIC_KEYCLOAK_MANAGEMENT_CLIENT_SECRET'];
const envKeys = allEnvKeys.filter((key) => !excludeKeys.includes(key));
// Step 2: Get all environment keys (from `process.env` and `import.meta.env`)
const allEnvKeys = [...Object.keys(process.env), ...Object.keys(import.meta.env)];
// Step 3: Separate "exposed" keys and "excluded" keys
const exposedEnvKeys = allEnvKeys.filter((key) => !excludeKeys.includes(key));
const excludedEnvKeys = allEnvKeys.filter((key) => excludeKeys.includes(key));
envKeys.forEach((item) => {
// Step 4: Store values for all keys in `initData`
exposedEnvKeys.forEach((item) => {
initData[item] = process.env[item] || import.meta.env[item];
});
// Step 5: Store **excluded keys** in a separate object (only for server-side use)
const excludedEnvData: any = {};
excludedEnvKeys.forEach((item) => {
excludedEnvData[item] = process.env[item] || import.meta.env[item];
});
const sessionToken = getFromCookies(Astro.cookies, 'session');
const refreshToken = getFromCookies(Astro.cookies, 'refresh');
---
Expand Down Expand Up @@ -73,9 +84,9 @@ const refreshToken = getFromCookies(Astro.cookies, 'refresh');

<script
id="global"
define:vars={{ initData, envKeys, sessionToken, refreshToken }}
define:vars={{ initData, exposedEnvKeys, sessionToken, refreshToken }}
>
envKeys.forEach((item) => {
exposedEnvKeys.forEach((item) => {
globalThis[item] = initData[item];
});

Expand Down Expand Up @@ -105,4 +116,4 @@ const refreshToken = getFromCookies(Astro.cookies, 'refresh');
}
</style>
</body>
</html>
</html>

0 comments on commit d084df7

Please sign in to comment.