Skip to content

Commit

Permalink
fix: env variable prod (#89)
Browse files Browse the repository at this point in the history
* chore: fix reading env variable

* bumb: version to 2.1.3
  • Loading branch information
laurentC35 authored Feb 26, 2024
1 parent 7d561b3 commit 072e881
Show file tree
Hide file tree
Showing 11 changed files with 10 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ dist-ssr
*.local

# Temporary env files
env-config.js
/env-config.js
4 changes: 1 addition & 3 deletions container/env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,4 @@ do
echo " $varname: \"$value\"," >> ./env-config.js
done < .env

echo "}" >> ./env-config.js

sed -i.bak 's~<body[^>]*>~&<script src="/env-config.js"></script>~' ./index.html
echo "}" >> ./env-config.js
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="/manifest.json" />
<script src="/env-config.js"></script>
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "public-enemy",
"version": "2.1.2",
"version": "2.1.3",
"description": "Check and test questionnaire models",
"repository": {
"type": "git",
Expand Down
1 change: 0 additions & 1 deletion public/configuration.json

This file was deleted.

1 change: 1 addition & 0 deletions public/env-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if (!window._env_) window._env_ = {};
9 changes: 3 additions & 6 deletions src/core/application/auth/provider/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import { getEnvVar } from "core/utils/configuration";
import { createOidcProvider } from "oidc-spa/react";
import React from "react";

export enum AuthType {
OIDC = "oidc",
NONE = "none",
}
export type AuthType = "oidc" | "none";

export const authType = getEnvVar("VITE_AUTH_TYPE");
export const authType = getEnvVar("VITE_AUTH_TYPE") as AuthType;
export const oidcConf = {
authUrl: getEnvVar("VITE_AUTH_URL"),
realm: getEnvVar("VITE_REALM"),
Expand Down Expand Up @@ -35,7 +32,7 @@ const { authUrl, realm, client_id } = oidcConf;
export const AuthContext = React.createContext(dummyOidcClient);

export function AuthProvider({ children }: { children: React.ReactNode }) {
if (authType === AuthType.OIDC) {
if (authType === "oidc") {
const { OidcProvider } = createOidcProvider({
issuerUri: `${authUrl}/realms/${realm}`,
clientId: client_id,
Expand Down
5 changes: 2 additions & 3 deletions src/core/infrastructure/hooks/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
AuthContext,
AuthType,
authType,
} from "core/application/auth/provider/component";
import { createUseOidc } from "oidc-spa/react";
Expand All @@ -9,7 +8,7 @@ import { useContext, useMemo } from "react";
export const { useOidc } = createUseOidc();

export const useAuth = () => {
if (authType === AuthType.OIDC) {
if (authType === "oidc") {
const oidc = useOidc();
return { oidc };
} else {
Expand All @@ -28,7 +27,7 @@ export const useUser = () => {
const { decodedIdToken } = oidc.oidcTokens;

const user = useMemo(() => {
if (authType === AuthType.OIDC) return decodedIdToken;
if (authType === "oidc") return decodedIdToken;
return { preferred_username: null, sub: "", timbre: "", name: "Fake User" };
}, [decodedIdToken]);

Expand Down
5 changes: 0 additions & 5 deletions src/core/utils/configuration/get-configuration.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/core/utils/configuration/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { getEnvVar } from "./env";
export { getConfiguration } from "./get-configuration";
15 changes: 0 additions & 15 deletions src/ui/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { CssBaseline } from "@mui/material";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { AuthProvider } from "core/application/auth/provider/component";
import { LocaleType, getMessages } from "core/i18n/messages";
import { getConfiguration } from "core/utils/configuration";
import { getEnvVar } from "core/utils/configuration/env";
import { SnackbarProvider } from "notistack";
import React from "react";
Expand All @@ -17,20 +16,6 @@ import reportWebVitals from "../reportWebVitals";
import { Application } from "./root/Application";
import { appTheme } from "./theme";

/* /!\ used in production mode where env config is used after build time
should not be used in docker environment
*/
await getConfiguration()
.then((conf) => {
if (Object.keys(conf).length) {
console.log("plop");
window._env_ = conf;
}
})
.catch((e) => {
console.log(e);
});

const queryClient = new QueryClient({
defaultOptions: {
queries: {
Expand Down

0 comments on commit 072e881

Please sign in to comment.