## Summary
Wire spec-driven auth into the frontend. Auth config (server URL, paths) is extracted from the served OpenAPI spec by `AppProvider`. `AuthProvider` receives the config as a prop. 401 responses from both the main API and the auth server dispatch an `auth:unauthorized` event that triggers redirect to `/login`. Fix `ProfileRoutes` URL duplication bug.
## Changes
### Auth config from spec
- **`main.jsx`** — `AppProvider` wraps everything, loads spec, exposes `authConfig`. `AuthProvider` receives `authConfig` from `useAppContext()`. `onUnauthorized` passed to both `AppProvider` and `AuthProvider` wires `navigate("/login")`.
### 401 handling
- **`AppProvider.tsx`** — remove `onUnauthorized` prop (handled by `AuthProvider`'s event listener instead, avoiding double-navigation).
- **`useApi.ts`** — 401 response interceptor dispatches `auth:unauthorized` CustomEvent on `window`.
### Profile routing fix
- **`Admin.tsx:ProfileRoutes`** — replace nested `<Routes>` (which caused `/profile/me/me` URL duplication with React Router v6) with `useLocation()`/`useNavigate()` conditional rendering. Only allows `/profile/me` and `/profile/me/edit`.
- **`Admin.tsx:ProfileComponentWrapper`** — replace `pushState() + reload()` with React Router `navigate()` for both `onEdit` and `handleSubmit`.
### Debug logging
- Temporary console logs at every navigation point for diagnosing remaining issues.
Reviewed-on: #12
Co-authored-by: Vishesh 'ironeagle' Bangotra <aetoskia@gmail.com>
Co-committed-by: Vishesh 'ironeagle' Bangotra <aetoskia@gmail.com>
163 lines
4.1 KiB
TypeScript
163 lines
4.1 KiB
TypeScript
import * as React from "react";
|
|
import {
|
|
useLocation,
|
|
matchPath
|
|
} from "react-router-dom";
|
|
import {
|
|
AppBar,
|
|
Toolbar,
|
|
Typography,
|
|
IconButton,
|
|
Tooltip,
|
|
Button,
|
|
Box,
|
|
useMediaQuery,
|
|
useTheme,
|
|
} from "@mui/material";
|
|
import MenuIcon from "@mui/icons-material/Menu";
|
|
import LogoutIcon from "@mui/icons-material/Logout";
|
|
import DarkModeIcon from "@mui/icons-material/DarkMode";
|
|
import LightModeIcon from "@mui/icons-material/LightMode";
|
|
import { useNavigate } from "react-router-dom";
|
|
import { useAuth } from "../react-auth";
|
|
import { ColorModeContext } from "./shared-theme/AppTheme";
|
|
|
|
interface HeaderProps {
|
|
routerMapping: {
|
|
path: string;
|
|
headerTitle: string;
|
|
}[];
|
|
onDrawerToggle?: () => void;
|
|
}
|
|
|
|
export default function Header({
|
|
routerMapping,
|
|
onDrawerToggle,
|
|
}: HeaderProps) {
|
|
const location = useLocation();
|
|
const matchedRoute = routerMapping.find((route) =>
|
|
matchPath({ path: route.path, end: false }, location.pathname)
|
|
);
|
|
const headerTitle = matchedRoute?.headerTitle ?? "Khata";
|
|
|
|
const navigate = useNavigate();
|
|
const { currentUser, logout } = useAuth();
|
|
const { mode, toggleColorMode } = React.useContext(ColorModeContext);
|
|
|
|
const theme = useTheme();
|
|
const isMobile = useMediaQuery(theme.breakpoints.down("md"));
|
|
|
|
const isAuthenticated = !!currentUser;
|
|
|
|
return (
|
|
<AppBar
|
|
position="fixed"
|
|
color="default"
|
|
sx={{
|
|
zIndex: (theme) => theme.zIndex.drawer + 1,
|
|
backdropFilter: "blur(8px)",
|
|
boxShadow: "none",
|
|
borderBottom: "1px solid",
|
|
borderColor: "divider",
|
|
}}
|
|
>
|
|
<Toolbar>
|
|
{/* MOBILE MENU BUTTON */}
|
|
{isMobile && onDrawerToggle && (
|
|
<IconButton
|
|
color="inherit"
|
|
edge="start"
|
|
onClick={onDrawerToggle}
|
|
sx={{ mr: 2 }}
|
|
>
|
|
<MenuIcon />
|
|
</IconButton>
|
|
)}
|
|
|
|
{/* THEME TOGGLE */}
|
|
<IconButton onClick={toggleColorMode} color="inherit" sx={{ mr: 2 }}>
|
|
{mode === 'dark' ? <LightModeIcon /> : <DarkModeIcon />}
|
|
</IconButton>
|
|
|
|
{/* TITLE */}
|
|
<Typography
|
|
variant="h6"
|
|
noWrap
|
|
sx={{ fontWeight: "bold", cursor: "pointer" }}
|
|
onClick={() => navigate("/")}
|
|
>
|
|
{headerTitle}
|
|
</Typography>
|
|
|
|
<span style={{ flexGrow: 1 }} />
|
|
|
|
{/* NAV LINKS */}
|
|
<Box
|
|
sx={{
|
|
display: { xs: "none", md: "flex" },
|
|
alignItems: "center",
|
|
mr: 2,
|
|
gap: 1,
|
|
}}
|
|
>
|
|
{[
|
|
{ label: "Fetch", path: "/fetch-requests" },
|
|
].map(({ label, path }) => (
|
|
<Button
|
|
key={path}
|
|
color="inherit"
|
|
onClick={() => navigate(path)}
|
|
sx={{ textTransform: "none", fontWeight: 500, px: 1.5 }}
|
|
size="small"
|
|
>
|
|
{label}
|
|
</Button>
|
|
))}
|
|
</Box>
|
|
|
|
{/* AUTH SECTION */}
|
|
{isAuthenticated ? (
|
|
<>
|
|
<Box
|
|
sx={{
|
|
display: { xs: "none", sm: "flex" },
|
|
alignItems: "center",
|
|
mr: 2,
|
|
}}
|
|
>
|
|
<Button
|
|
color="inherit"
|
|
onClick={() => navigate("/admin")}
|
|
sx={{ textTransform: "none", fontWeight: 500 }}
|
|
>
|
|
Admin
|
|
</Button>
|
|
<Button
|
|
color="inherit"
|
|
onClick={() => navigate("/profile/me")}
|
|
sx={{ textTransform: "none", fontWeight: 500 }}
|
|
>
|
|
{currentUser.username}
|
|
</Button>
|
|
</Box>
|
|
|
|
<Tooltip title="Logout">
|
|
<IconButton color="inherit" onClick={logout}>
|
|
<LogoutIcon />
|
|
</IconButton>
|
|
</Tooltip>
|
|
</>
|
|
) : (
|
|
<Button
|
|
color="inherit"
|
|
variant="outlined"
|
|
onClick={() => navigate("/login")}
|
|
sx={{ textTransform: "none" }}
|
|
>
|
|
Login
|
|
</Button>
|
|
)}
|
|
</Toolbar>
|
|
</AppBar>
|
|
);
|
|
} |