From 1cfb45c625fa5b9808dcdd68088cf22d33cf2700 Mon Sep 17 00:00:00 2001 From: Alexey Lunin <alexey.lunin@vereign.com> Date: Wed, 24 Apr 2024 12:41:59 +0300 Subject: [PATCH] Add ability to set Auth token on prod manually using local storage --- apps/dashboard/src/components/App/index.tsx | 2 +- apps/dashboard/src/hooks/auth/useAuth.ts | 7 +++++++ apps/dashboard/src/main.tsx | 2 +- apps/dashboard/src/utils/getConfig.ts | 3 +++ libs/clients/src/ocmengine-client.ts | 8 ++++---- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/apps/dashboard/src/components/App/index.tsx b/apps/dashboard/src/components/App/index.tsx index 4050dbcd..f539f1e7 100644 --- a/apps/dashboard/src/components/App/index.tsx +++ b/apps/dashboard/src/components/App/index.tsx @@ -20,7 +20,7 @@ const App = observer(() => { }, wsUrl: config.OCMENGINE_WS_URL, httpUrl: config.OCMENGINE_HTTP_URL, - getToken: async () => auth.getToken() || "", + getAuthorization: async () => config.OCMENGINE_AUTHORIZATION || `Bearer ${auth.getToken()}` || "", }); setAppLoading(false); }, [auth]); diff --git a/apps/dashboard/src/hooks/auth/useAuth.ts b/apps/dashboard/src/hooks/auth/useAuth.ts index 2eda3980..febc8e96 100644 --- a/apps/dashboard/src/hooks/auth/useAuth.ts +++ b/apps/dashboard/src/hooks/auth/useAuth.ts @@ -1,4 +1,5 @@ import { useEffect, useState } from "react"; +import getConfig from "@dashboard/utils/getConfig"; const LS_KEY = "token"; @@ -30,6 +31,12 @@ const useAuth = () => { const [loading, setLoading] = useState(true); useEffect(() => { + if (getConfig().OCMENGINE_AUTHORIZATION) { + setAuthorized(true); + setLoading(false); + return; + } + const lsToken = localStorage.getItem(LS_KEY); if (lsToken) { if (isJwtValid(lsToken)) { diff --git a/apps/dashboard/src/main.tsx b/apps/dashboard/src/main.tsx index 0e7223c9..9b91aa72 100644 --- a/apps/dashboard/src/main.tsx +++ b/apps/dashboard/src/main.tsx @@ -19,7 +19,7 @@ const config = getConfig(); setOcmEngineConfig({ wsUrl: config.OCMENGINE_WS_URL, httpUrl: config.OCMENGINE_HTTP_URL, - getToken: async () => "", + getAuthorization: async () => "", }); setTsaConfig({ diff --git a/apps/dashboard/src/utils/getConfig.ts b/apps/dashboard/src/utils/getConfig.ts index 835d16a7..467aac06 100644 --- a/apps/dashboard/src/utils/getConfig.ts +++ b/apps/dashboard/src/utils/getConfig.ts @@ -3,10 +3,12 @@ export interface Config { OCMENGINE_WS_URL: string; TSA_URL: string; BASE_PATH: string; + OCMENGINE_AUTHORIZATION: string | null | undefined; } const lsHttp = localStorage.getItem("OCMENGINE_HTTP_URL"); const lsWs = localStorage.getItem("OCMENGINE_WS_URL"); +const authorization = localStorage.getItem("OCMENGINE_AUTHORIZATION"); const tsaHttp = localStorage.getItem("TSA_URL"); export const getConfig = (): Config => { return { @@ -14,6 +16,7 @@ export const getConfig = (): Config => { OCMENGINE_WS_URL: lsWs || window.OCMENGINE_WS_URL, TSA_URL: tsaHttp || window.TSA_URL, BASE_PATH: window.BASE_PATH, + OCMENGINE_AUTHORIZATION: authorization }; }; diff --git a/libs/clients/src/ocmengine-client.ts b/libs/clients/src/ocmengine-client.ts index 42d2f94d..3f2b2e9d 100644 --- a/libs/clients/src/ocmengine-client.ts +++ b/libs/clients/src/ocmengine-client.ts @@ -27,7 +27,7 @@ export interface Config { onUnauthorized?: () => void; wsUrl: string; httpUrl: string; - getToken: () => Promise<string>; + getAuthorization: () => Promise<string>; } let config: Config | null = null; @@ -129,12 +129,12 @@ class ApiClient { // this._listen(); } - public async getToken(): Promise<string> { + public async getAuthorization(): Promise<string> { if (!config) { throw new Error("ApiClient: Please call setConfig before calling api"); } - return config.getToken(); + return config.getAuthorization(); } private async _fetch(url: RequestInfo, init?: RequestInit): Promise<unknown> { @@ -145,7 +145,7 @@ class ApiClient { init = Object.assign({}, init, { headers: { ...(init?.headers || {}), - Authorization: `Bearer ${await config.getToken()}`, + Authorization: await config.getAuthorization(), }, }); -- GitLab