Untitled

 avatar
unknown
plain_text
a year ago
1.0 kB
5
Indexable
"use client";
import { useEffect } from "react";
import { useRouter } from "next/navigation";
import NotFound from "@/app/not-found";
import Loading from "@/app/loading";
import { useAuth } from "@/hooks/use-auth";

interface AuthProviderProps {
  children: React.ReactNode;
  type: "public" | "private";
}

export function AuthProvider({ children, type }: AuthProviderProps) {
  const router = useRouter();
  const { isSignedIn, isPending } = useAuth();

  useEffect(() => {
    if (type === "public" && isSignedIn) {
      router.replace("/dashboard");
    }
  }, [isSignedIn, router, type]);

  if (isPending) {
    return <Loading />;
  }

  if (isSignedIn === true && type === "private") {
    return <>{children}</>;
  }

  if (isSignedIn === false && type === "public") {
    return <>{children}</>;
  }

  if (isSignedIn === false && type === "private") {
    return <NotFound />;
  }

  // ufeEffect will automatically redirect if isAuthenticated === true && type === public

  return null;
}
Editor is loading...
Leave a Comment