How to keep user logged in after refresh?

Hi, after searching and going through so many forums, as non of them worked I tried asking here.
How do I keep the user stay logged in and check if the user is not logged in, they can’t access particular link. I tried doing it like this but manually refreshing the page, pushes user out.

  useEffect(() => {
    if (!isAuthenticated) {
      router.replace("/");
    }
  }, [isAuthenticated, router]);

I also tried using Moralis.User.current() but it won’t work as intended either, as it never pushes back to "/" according to last condition of if (!checkUser).

  useEffect(() => {
    const checkUser = async () => {
      if (isInitialized) {
        Moralis.User.current();
      }
    };
    if (!checkUser) {
      router.replace("/");
    }
  }, [isAuthenticated, router, Moralis, isInitialized]);

Thanks

You can look at this