Files
OpenFlare/openflare_server/web/features/auth/components/dashboard-auth-guard.tsx
T
2026-05-30 13:15:20 +08:00

39 lines
1.0 KiB
TypeScript

'use client';
import type { ReactNode } from 'react';
import { useEffect } from 'react';
import { usePathname, useRouter } from 'next/navigation';
import { LoadingState } from '@/components/feedback/loading-state';
import { useAuth } from '@/components/providers/auth-provider';
interface DashboardAuthGuardProps {
children: ReactNode;
}
export function DashboardAuthGuard({ children }: DashboardAuthGuardProps) {
const router = useRouter();
const pathname = usePathname();
const { isAuthenticated, isLoading } = useAuth();
useEffect(() => {
if (!isLoading && !isAuthenticated) {
const redirect =
pathname && pathname !== '/'
? `?redirect=${encodeURIComponent(pathname)}`
: '';
router.replace(`/login${redirect}`);
}
}, [isAuthenticated, isLoading, pathname, router]);
if (isLoading || !isAuthenticated) {
return (
<main className="flex min-h-screen items-center justify-center px-4 py-8">
<LoadingState className="w-full max-w-md" />
</main>
);
}
return <>{children}</>;
}