Skip to content
Snippets Groups Projects
Commit 1cfb45c6 authored by Alexey Lunin's avatar Alexey Lunin
Browse files

Add ability to set Auth token on prod manually using local storage

parent 5cf830c7
No related branches found
No related tags found
1 merge request!80fix: add ability to set Auth token on prod manually using local storage
Pipeline #71027 waiting for manual action
......@@ -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]);
......
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)) {
......
......@@ -19,7 +19,7 @@ const config = getConfig();
setOcmEngineConfig({
wsUrl: config.OCMENGINE_WS_URL,
httpUrl: config.OCMENGINE_HTTP_URL,
getToken: async () => "",
getAuthorization: async () => "",
});
setTsaConfig({
......
......@@ -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
};
};
......
......@@ -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(),
},
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment