Files
edr-platform/apps/edr-freight-web/portal/src/main.tsx
Michael Abebe 2478b0278b feat(freight:auth): show real user profile in top bar and fix logout
- DashboardLayout now accepts userName, userEmail, userInitials, onLogout
  props; computes initials from name; replaces hardcoded "John Doe" values
- Portal App.tsx pulls display name and email from useAuth user object and
  passes them to DashboardLayout
- handleLogout performs unconditional local cleanup (cookies + localStorage
  + hard redirect to /auth) so a failed API call never leaves the user stuck
- main.tsx purges cookies whose value is the literal string "undefined",
  clearing stale tokens written before the envelope interceptor fix

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-18 11:52:03 +03:00

82 lines
2.4 KiB
TypeScript

import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { BrowserRouter } from "react-router-dom";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import "@tria-plc/iamui-common/styles.css";
import App from "./App";
import {
AuthProvider,
BrandingProvider,
configureIam,
UserProvider,
axiosInstance,
} from "@tria-plc/iamui-common";
// Purge cookies that were stored as the literal string "undefined" before the
// envelope interceptor fix. Without this, stale sessions would keep sending
// "Authorization: Bearer undefined" and get 401 JsonWebTokenError forever.
["auth-token", "refresh-token"].forEach((name) => {
const entry = document.cookie
.split("; ")
.find((c) => c.startsWith(`${name}=`));
const value = entry?.split("=").slice(1).join("=");
if (value === "undefined" || value === "null" || value === "") {
document.cookie = `${name}=; Max-Age=0; path=/`;
}
});
const queryClient = new QueryClient();
window.__IAM_CONFIG__ = {
apiUrl: `${import.meta.env.VITE_BASE_API_URL}/api`,
postLoginPath: "/",
};
// Unwrap the StandardResponse envelope ({ success, data, timestamp }) that the
// freight API's ResponseTransformInterceptor adds to every response, so that
// iamui-common can read response.data.token / response.data fields as expected.
axiosInstance.interceptors.response.use((response) => {
if (
response.data &&
typeof response.data === "object" &&
"success" in response.data &&
"data" in response.data
) {
response.data = response.data.data;
}
return response;
});
window.__USER_MANAGEMENT_BRANDING__ = {
organizationName: "EDR Platform",
appName: "EDR Portal",
moduleBasePath: "/user-management",
backToAppPath: "/dashboard",
backToAppLabel: "Back to dashboard",
};
configureIam({
apiUrl: `${import.meta.env.VITE_BASE_API_URL}/api`,
});
const rootElement = document.getElementById("root");
if (!rootElement) {
throw new Error("Root element not found");
}
createRoot(document.getElementById("root")!).render(
<StrictMode>
<QueryClientProvider client={queryClient}>
<BrowserRouter>
<BrandingProvider>
<AuthProvider>
<UserProvider>
<App />
</UserProvider>
</AuthProvider>
</BrandingProvider>
</BrowserRouter>
</QueryClientProvider>
</StrictMode>,
);