mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-30 14:26:36 +08:00
39 lines
1.0 KiB
TypeScript
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}</>;
|
|
}
|