mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
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:
@@ -1,3 +1,4 @@
|
||||
import "./Utils/API";
|
||||
import App from "./App";
|
||||
import Telemetry from "Common/UI/Utils/Telemetry/Telemetry";
|
||||
import React from "react";
|
||||
|
||||
42
AdminDashboard/src/Utils/API.ts
Normal file
42
AdminDashboard/src/Utils/API.ts
Normal 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;
|
||||
Reference in New Issue
Block a user