diff --git a/AdminDashboard/src/Index.tsx b/AdminDashboard/src/Index.tsx index 6fde030972..c75ca25573 100644 --- a/AdminDashboard/src/Index.tsx +++ b/AdminDashboard/src/Index.tsx @@ -1,3 +1,4 @@ +import "./Utils/API"; import App from "./App"; import Telemetry from "Common/UI/Utils/Telemetry/Telemetry"; import React from "react"; diff --git a/AdminDashboard/src/Utils/API.ts b/AdminDashboard/src/Utils/API.ts new file mode 100644 index 0000000000..158935d5af --- /dev/null +++ b/AdminDashboard/src/Utils/API.ts @@ -0,0 +1,42 @@ +import BaseAPI from "Common/UI/Utils/API/API"; +import { IDENTITY_URL } from "Common/UI/Config"; +import HTTPErrorResponse from "Common/Types/API/HTTPErrorResponse"; +import URL from "Common/Types/API/URL"; +import { JSONObject } from "Common/Types/JSON"; +import { Logger } from "Common/UI/Utils/Logger"; + +const registerAdminDashboardAuthRefresh = (): void => { + const refreshSession = async (): Promise => { + try { + const response = await BaseAPI.post({ + url: URL.fromURL(IDENTITY_URL).addRoute("/refresh-session"), + options: { + skipAuthRefresh: true, + }, + }); + + if (response instanceof HTTPErrorResponse) { + Logger.warn( + `Admin dashboard session refresh failed with status ${response.statusCode}.`, + ); + return false; + } + + return response.isSuccess(); + } catch (err) { + Logger.error("Admin dashboard session refresh request failed."); + Logger.error(err as Error); + return false; + } + }; + + BaseAPI.setRefreshSessionHandler(refreshSession); + + BaseAPI.setRefreshFailureHandler(() => { + Logger.warn("Admin dashboard session refresh failed; falling back to logout."); + }); +}; + +registerAdminDashboardAuthRefresh(); + +export default BaseAPI;