Files
factoryOps-v2/dashboard/components/AuthGuard.tsx
Johngreen ab2a3e35b2 feat: Phase 0-2 complete — auth, machines, equipment parts with full CRUD
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>
2026-02-10 12:05:22 +09:00

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}</>;
}