Files
stack/apps/web/src/spa/guards.tsx
T
fred 2f41bb5fb7
ci/woodpecker/pr/ci Pipeline was successful
feat(web): port settings and admin surfaces into the SPA (Phase P4-2)
Ports the legacy Next settings page (profile/appearance/notifications/
providers tabs) and admin page (user management + system health) into
spa/pages as faithful ports, replaces the /settings and /admin route
placeholders, and adds a route-level AdminGuard mirroring the legacy
AdminRoleGuard semantics (unauthenticated -> /login, non-admin -> /).
Legacy app/ tree untouched; removal stays in P5 per the increment map.
2026-08-26 17:55:40 -05:00

44 lines
1.1 KiB
TypeScript

import type { ReactElement } from 'react';
import { Navigate, Outlet } from 'react-router-dom';
import { useSession } from '@/lib/auth-client';
export function GuestGuard(): ReactElement {
const { data: session } = useSession();
return session ? <Navigate to="/chat" replace /> : <Outlet />;
}
export function AuthGuard(): ReactElement {
const { data: session, isPending } = useSession();
if (isPending) {
return (
<div className="flex min-h-screen items-center justify-center">
<div className="text-sm text-text-muted">Loading...</div>
</div>
);
}
return session ? <Outlet /> : <Navigate to="/login" replace />;
}
export function AdminGuard(): ReactElement {
const { data: session, isPending } = useSession();
if (isPending) {
return (
<div className="flex min-h-screen items-center justify-center">
<div className="text-sm text-text-muted">Loading...</div>
</div>
);
}
if (!session) {
return <Navigate to="/login" replace />;
}
const user = session.user as typeof session.user & { role?: string };
return user.role === 'admin' ? <Outlet /> : <Navigate to="/" replace />;
}