fix:(api): auth redirect

This commit is contained in:
Michael Abebe
2026-05-18 10:46:09 +03:00
parent 0e0d2ea450
commit 4256a08917
3 changed files with 32 additions and 6 deletions

View File

@@ -16,7 +16,7 @@ import TrackingPage from "./pages/tracking/TrackingPage";
import BillingPage from "./pages/billing/BillingPage";
import TrainsPage from "./pages/trains/TrainsPage";
import DashboardPage from "./pages/dashboard/DashboardPage";
import { IamLoginPage } from "@tria-plc/iamui-common";
import { IamLoginPage, LoadingScreen, useAuth } from "@tria-plc/iamui-common";
const sidebarItems: SidebarItem[] = [
{ label: "Dashboard", href: "/" },
@@ -30,8 +30,13 @@ const sidebarItems: SidebarItem[] = [
const App = () => {
const navigate = useNavigate();
const location = useLocation();
const { user, loading } = useAuth();
if (location.pathname === "/auth") {
if (loading) {
return <LoadingScreen />;
}
if (!user) {
return (
<Routes>
<Route path="/auth" element={<IamLoginPage />} />
@@ -40,6 +45,10 @@ const App = () => {
);
}
if (location.pathname === "/auth") {
return <Navigate to="/" replace />;
}
return (
<DashboardLayout
title="EDR Freight"

View File

@@ -15,7 +15,7 @@ import {
const queryClient = new QueryClient();
window.__IAM_CONFIG__ = {
apiUrl: `${import.meta.env.VITE_BASE_API_URL}/api`,
postLoginPath: "/user-management",
postLoginPath: "/",
};
window.__USER_MANAGEMENT_BRANDING__ = {
organizationName: "EDR Platform",

View File

@@ -4,6 +4,23 @@ export const api = axios.create({
baseURL: import.meta.env.VITE_API_URL,
});
// TODO: integrate @edr/auth — add a request interceptor here that attaches
// the bearer token from the auth package and a response interceptor that
// triggers a refresh on 401.
api.interceptors.request.use((config) => {
const token = document.cookie
.split("; ")
.find((row) => row.startsWith("auth-token="))
?.split("=")[1];
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
api.interceptors.response.use(
(response) => response,
(error) => {
if (error.response?.status === 401) {
window.location.href = "/auth";
}
return Promise.reject(error);
},
);