47 lines
1.0 KiB
TypeScript
47 lines
1.0 KiB
TypeScript
|
|
"use client";
|
||
|
|
|
||
|
|
import { useEffect, useState } from "react";
|
||
|
|
import { useRouter } from "next/navigation";
|
||
|
|
import { useLotteryAuth } from "@/store/useLotteryAuth";
|
||
|
|
import Loader from "@/components/Loader";
|
||
|
|
|
||
|
|
interface ProtectedRouteProps {
|
||
|
|
children: React.ReactNode;
|
||
|
|
}
|
||
|
|
|
||
|
|
const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
|
||
|
|
const router = useRouter();
|
||
|
|
const { isAuthenticated } = useLotteryAuth();
|
||
|
|
const [isHydrated, setIsHydrated] = useState(false);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
setIsHydrated(true);
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
if (isHydrated && !isAuthenticated) {
|
||
|
|
router.replace("/lottery/auth");
|
||
|
|
}
|
||
|
|
}, [isHydrated, isAuthenticated, router]);
|
||
|
|
|
||
|
|
if (!isHydrated) {
|
||
|
|
return (
|
||
|
|
<div className="w-full h-screen flex justify-center items-center">
|
||
|
|
<Loader />
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!isAuthenticated) {
|
||
|
|
return (
|
||
|
|
<div className="w-full h-screen flex justify-center items-center">
|
||
|
|
<Loader />
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
return <>{children}</>;
|
||
|
|
};
|
||
|
|
|
||
|
|
export default ProtectedRoute;
|