Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dev": "vite",
"build": "tsc --noEmit && vite build",
"typecheck": "tsc --noEmit",
"test": "node --test src/api/client.test.ts src/lib/utils.test.ts",
"preview": "vite preview"
},
"dependencies": {
Expand Down
22 changes: 19 additions & 3 deletions apps/web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { lazy, type ReactNode, Suspense } from 'react';
import { Link, Navigate, Route, Routes } from 'react-router-dom';
import { Link, Navigate, Route, Routes, useLocation } from 'react-router-dom';
import { Button } from './components/ui/button.tsx';
import { useAuth } from './contexts/AuthContext.tsx';
import { useTranslation } from './i18n/index.tsx';
import { buildLoginRedirect } from './lib/utils.ts';

const AdminLayout = lazy(() => import('./components/layout/AdminLayout.tsx'));
const DashboardLayout = lazy(
Expand Down Expand Up @@ -51,17 +52,32 @@ function SessionRestoreFailed() {

function ProtectedRoute({ children }: { children: ReactNode }) {
const { user, isLoading, restoreFailed } = useAuth();
const location = useLocation();
if (isLoading) return <PageFallback />;
if (!user && restoreFailed) return <SessionRestoreFailed />;
if (!user) return <Navigate to="/login" replace />;
// FE-R5: remember where the user was so signing in returns them there.
if (!user)
return (
<Navigate
to={buildLoginRedirect(location.pathname, location.search)}
replace
/>
);
return <>{children}</>;
}

function AdminRoute({ children }: { children: ReactNode }) {
const { user, isLoading, restoreFailed } = useAuth();
const location = useLocation();
if (isLoading) return <PageFallback />;
if (!user && restoreFailed) return <SessionRestoreFailed />;
if (!user) return <Navigate to="/login" replace />;
if (!user)
return (
<Navigate
to={buildLoginRedirect(location.pathname, location.search)}
replace
/>
);
if (user.role !== 'admin') return <Navigate to="/dashboard" replace />;
return <>{children}</>;
}
Expand Down
Loading
Loading