diff --git a/components/auth/auth-check.tsx b/components/auth/auth-check.tsx index 603c397..b0176cc 100644 --- a/components/auth/auth-check.tsx +++ b/components/auth/auth-check.tsx @@ -2,28 +2,32 @@ import { useSession } from "next-auth/react"; import { usePathname, useRouter } from "next/navigation"; -import { useEffect, useState } from "react"; +import { useEffect } from "react"; export function AuthCheck({ children }: { children: React.ReactNode }) { const { data: session, status } = useSession(); const pathname = usePathname(); const router = useRouter(); - const [isRedirecting, setIsRedirecting] = useState(false); useEffect(() => { - if (status === "unauthenticated" && pathname !== "/signin" && !isRedirecting) { - setIsRedirecting(true); - router.push("/signin"); + // Only redirect if we're certain the user is not authenticated + // and we're not already on the signin page + if (status === "unauthenticated" && pathname !== "/signin") { + router.replace("/signin"); } - }, [status, router, pathname, isRedirecting]); + }, [status, pathname, router]); + // During loading, show the children to prevent flashing + // This works because server-side session check will handle protection if (status === "loading") { - return
Chargement...
; + return <>{children}; } - if (status === "unauthenticated" && pathname !== "/signin") { - return null; + // If we're on the signin page, or if we're authenticated, show the children + if (pathname === "/signin" || status === "authenticated") { + return <>{children}; } - return <>{children}; + // Otherwise, render nothing while redirecting + return null; } \ No newline at end of file diff --git a/components/auth/signin-form.tsx b/components/auth/signin-form.tsx index d43f28f..7ddabbc 100644 --- a/components/auth/signin-form.tsx +++ b/components/auth/signin-form.tsx @@ -8,7 +8,7 @@ export function SignInForm() {

Bienvenue sur Enkun

Connectez-vous pour accéder à votre espace