2022-10-10 10:14:19 +00:00
|
|
|
export type Environment = {
|
2022-11-03 14:06:26 +00:00
|
|
|
/** The URL to the root of the auth server. */
|
2023-04-25 11:11:20 +00:00
|
|
|
authUrl: string;
|
2022-11-03 14:06:26 +00:00
|
|
|
/** Indicates if the application is running as a Keycloak theme. */
|
|
|
|
isRunningAsTheme: boolean;
|
2023-04-25 11:11:20 +00:00
|
|
|
/** The realm used to sign into. */
|
|
|
|
realm: string;
|
|
|
|
/** The URL to resources such as the files in the `public` directory. */
|
|
|
|
resourceUrl: string;
|
2022-10-10 10:14:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// The default environment, used during development.
|
|
|
|
const defaultEnvironment: Environment = {
|
2023-04-25 11:11:20 +00:00
|
|
|
authUrl: "http://localhost:8180",
|
2022-11-03 14:06:26 +00:00
|
|
|
isRunningAsTheme: false,
|
2023-04-25 11:11:20 +00:00
|
|
|
realm: "master",
|
|
|
|
resourceUrl: "http://localhost:8080",
|
2022-10-10 10:14:19 +00:00
|
|
|
};
|
|
|
|
|
2023-04-13 13:41:40 +00:00
|
|
|
// Merge the default and injected environment variables together.
|
|
|
|
const environment: Environment = {
|
|
|
|
...defaultEnvironment,
|
|
|
|
...getInjectedEnvironment(),
|
|
|
|
};
|
|
|
|
|
|
|
|
export { environment };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts the environment variables that are passed if the application is running as a Keycloak theme.
|
|
|
|
* These variables are injected by Keycloak into the `index.ftl` as a script tag, the contents of which can be parsed as JSON.
|
|
|
|
*/
|
|
|
|
function getInjectedEnvironment(): Record<string, string | number | boolean> {
|
|
|
|
const element = document.getElementById("environment");
|
|
|
|
|
|
|
|
// If the element cannot be found, return an empty record.
|
|
|
|
if (!element?.textContent) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to parse the contents as JSON and return its value.
|
|
|
|
try {
|
|
|
|
return JSON.parse(element.textContent);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Unable to parse environment variables.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, return an empty record.
|
|
|
|
return {};
|
|
|
|
}
|