On Page Refresh, it goes to login page ( Nextjs + Zustand )
Unanswered
Siberian Accentor posted this in #help-forum
Original message was deleted.
3 Replies
Siberian Accentor
"use client";
import { useRouter } from "next/navigation";
import { useEffect } from "react";
import UserStore from "@/zustand-store/user-store";
function AuthGuard({ children }: { children: React.ReactNode }) {
const router = useRouter();
const { token, isLoggedIn } = UserStore();
const isAuthenticated = isLoggedIn;
useEffect(() => {
if (!isAuthenticated) {
router.push("/login");
console.log("Not Authenticated");
}
}, [isAuthenticated, router, token]);
return isAuthenticated ? <> {children} </> : null;
}
export default AuthGuard;then on layout.tsx
<AuthGuard>
{children }
</AuthGuard>Siberian Accentor
it redirect me to login for sec and comeback, why is that ? can anyone help me ?