feat(api): register admin-dashboard auth refresh handler

Add AdminDashboard/src/Utils/API.ts to register a refreshSession handler on BaseAPI
that posts to IDENTITY_URL/refresh-session (using skipAuthRefresh). Handle HTTP error
responses and exceptions, and set a refresh-failure handler that logs and falls back
to logout. Export BaseAPI. Import the module in Index.tsx for side-effect initialization.
This commit is contained in:
Nawaz Dhandala
2025-11-06 15:01:32 +00:00
parent c41b53dd2a
commit 5e0d8b487c
2 changed files with 43 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
import "./Utils/API";
import App from "./App";
import Telemetry from "Common/UI/Utils/Telemetry/Telemetry";
import React from "react";

View File

@@ -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<boolean> => {
try {
const response = await BaseAPI.post<JSONObject>({
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;