Co-authored-by: Jason Woltje <jason@diversecanvas.com> Co-committed-by: Jason Woltje <jason@diversecanvas.com>
35 lines
762 B
TypeScript
35 lines
762 B
TypeScript
'use client';
|
|
|
|
import { useRouter } from 'next/navigation';
|
|
import { useEffect } from 'react';
|
|
import { useSession } from '@/lib/auth-client';
|
|
|
|
interface AuthGuardProps {
|
|
children: React.ReactNode;
|
|
}
|
|
|
|
export function AuthGuard({ children }: AuthGuardProps): React.ReactElement | null {
|
|
const { data: session, isPending } = useSession();
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
if (!isPending && !session) {
|
|
router.replace('/login');
|
|
}
|
|
}, [isPending, session, router]);
|
|
|
|
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 null;
|
|
}
|
|
|
|
return <>{children}</>;
|
|
}
|