diff --git a/apps/edr-freight-web/portal/src/App.tsx b/apps/edr-freight-web/portal/src/App.tsx index 6b788db70..e1482c142 100644 --- a/apps/edr-freight-web/portal/src/App.tsx +++ b/apps/edr-freight-web/portal/src/App.tsx @@ -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 ; + } + + if (!user) { return ( } /> @@ -40,6 +45,10 @@ const App = () => { ); } + if (location.pathname === "/auth") { + return ; + } + return ( { + 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); + }, +);