Multi-tenant factory inspection system (SpiFox, Enkid, Alpet): - FastAPI backend with JWT auth, PostgreSQL (asyncpg) - Next.js 16 frontend with App Router, SWR data fetching - Machines CRUD with equipment parts management - Part lifecycle tracking (hours/count/date) with counters - Partial unique index for soft-delete support - 24 pytest tests passing, E2E verified Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
'use client';
|
|
|
|
import { useEffect } from 'react';
|
|
import { useRouter, usePathname } from 'next/navigation';
|
|
import { useAuth } from '@/lib/auth-context';
|
|
|
|
const PUBLIC_PATHS = ['/login'];
|
|
|
|
export function AuthGuard({ children }: { children: React.ReactNode }) {
|
|
const { isAuthenticated, isLoading } = useAuth();
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
|
|
const isPublicPath = PUBLIC_PATHS.includes(pathname);
|
|
|
|
useEffect(() => {
|
|
if (isLoading) return;
|
|
|
|
if (!isAuthenticated && !isPublicPath) {
|
|
router.push('/login');
|
|
} else if (isAuthenticated && isPublicPath) {
|
|
router.push('/');
|
|
}
|
|
}, [isAuthenticated, isLoading, isPublicPath, router]);
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="auth-loading">
|
|
<span className="material-symbols-outlined spinning">progress_activity</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (!isAuthenticated && !isPublicPath) {
|
|
return null;
|
|
}
|
|
|
|
if (isAuthenticated && isPublicPath) {
|
|
return null;
|
|
}
|
|
|
|
return <>{children}</>;
|
|
}
|