Skip to content

Commit

Permalink
fix: env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
MOUAD EL AZAAR committed Feb 9, 2025
1 parent b69bf9c commit 5c6dc6a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
23 changes: 9 additions & 14 deletions src/envVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,19 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

type EnvVariableType = {
export type EnvVariableType = {
VITE_PEGASE_OAUTH2_CLIENT_ID: string;
VITE_PEGASE_OAUTH2_REDIRECT_URL: string;
VITE_PEGASE_OAUTH2_AUTHORITY: string;
VITE_BACK_END_BASE_URL: string;
VITE_OAUTH2_CLIENT_ID: string;
VITE_OAUTH2_REDIRECT_URL: string;
VITE_AUTHORITY: string;
};

// Environment Variable Template to Be Replaced at Runtime
export const envVariables: EnvVariableType = {
VITE_PEGASE_OAUTH2_CLIENT_ID: '${PEGASE_OAUTH2_CLIENT_ID}',
VITE_PEGASE_OAUTH2_REDIRECT_URL: '${PEGASE_OAUTH2_REDIRECT_URL}',
VITE_PEGASE_OAUTH2_AUTHORITY: '${PEGASE_OAUTH2_AUTHORITY}',
VITE_BACK_END_BASE_URL: '${URL_BACKEND}',
VITE_OAUTH2_CLIENT_ID: '${PEGASE_OAUTH2_CLIENT_ID}',
VITE_OAUTH2_REDIRECT_URL: '${PEGASE_OAUTH2_REDIRECT_URL}',
VITE_AUTHORITY: '${PEGASE_AUTHORITY}',
};
export const getEnvVariables = (key: keyof EnvVariableType): string => {
if (envVariables[key].startsWith('$')) {
return import.meta.env[key] as string;
} else {
return envVariables[key];
}
};
export const getEnvVariables = (key: keyof EnvVariableType) =>
envVariables[key].startsWith('$') ? (import.meta.env[key] as string) : envVariables[key];
2 changes: 1 addition & 1 deletion src/shared/const/apiEndPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { getEnvVariables } from '@/envVariables.ts';

const BASE_URL = getEnvVariables('VITE_BACK_END_BASE_URL');
const BASE_URL = getEnvVariables('VITE_BACK_END_BASE_URL').trim();

// STUDY
export const STUDY_ENDPOINT = `${BASE_URL}/v1/study`;
Expand Down
8 changes: 4 additions & 4 deletions src/shared/services/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { User, UserManager } from 'oidc-client-ts';
import { getEnvVariables } from '@/envVariables.ts';
import { EnvVariableType, getEnvVariables } from '@/envVariables';

interface AuthConfig {
authority: string;
Expand All @@ -16,9 +16,9 @@ interface AuthConfig {
}

const config: AuthConfig = {
client_id: getEnvVariables('VITE_OAUTH2_CLIENT_ID'),
redirect_uri: getEnvVariables('VITE_OAUTH2_REDIRECT_URL'),
authority: getEnvVariables('VITE_OAUTH2_REDIRECT_URL'),
client_id: getEnvVariables('VITE_OAUTH2_CLIENT_ID' as keyof EnvVariableType).trim(),
redirect_uri: getEnvVariables('VITE_OAUTH2_REDIRECT_URL' as keyof EnvVariableType).trim(),
authority: getEnvVariables('VITE_PEGASE_OAUTH2_AUTHORITY' as keyof EnvVariableType).trim(),
scope: 'openid email profile',
maxExpiresIn: 600,
};
Expand Down

0 comments on commit 5c6dc6a

Please sign in to comment.