From 488587165757330b456ac114b1a9cb20caa518f1 Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Tue, 30 Sep 2025 23:37:37 +0200 Subject: [PATCH] feat: enhance login and registration pages with session handling - Added `useEffect` to redirect authenticated users to the home page in both `LoginPage` and `RegisterPage`. - Integrated `useSession` from `next-auth/react` to manage session state and loading indicators. - Implemented loading state display while checking session status, improving user experience during authentication. - Prevented form display for authenticated users, streamlining the login and registration process. --- src/app/login/page.tsx | 29 +++++++++++++++++++++++++++-- src/app/register/page.tsx | 28 +++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index 34ee37f..dc66ef1 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -1,7 +1,7 @@ 'use client' -import { useState } from 'react' -import { signIn, getSession } from 'next-auth/react' +import { useState, useEffect } from 'react' +import { signIn, getSession, useSession } from 'next-auth/react' import { useRouter } from 'next/navigation' import Link from 'next/link' import { Button } from '@/components/ui/Button' @@ -15,6 +15,14 @@ export default function LoginPage() { const [error, setError] = useState('') const [isLoading, setIsLoading] = useState(false) const router = useRouter() + const { data: session, status } = useSession() + + // Redirection si l'utilisateur est déjà connecté + useEffect(() => { + if (status === 'authenticated' && session) { + router.push('/') + } + }, [status, session, router]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() @@ -44,6 +52,23 @@ export default function LoginPage() { } } + // Afficher un loader pendant la vérification de session + if (status === 'loading') { + return ( +
+ +
+
Chargement...
+
+
+ ) + } + + // Ne pas afficher le formulaire si l'utilisateur est connecté + if (status === 'authenticated') { + return null + } + return (
diff --git a/src/app/register/page.tsx b/src/app/register/page.tsx index 54c9e65..46b78a4 100644 --- a/src/app/register/page.tsx +++ b/src/app/register/page.tsx @@ -1,7 +1,8 @@ 'use client' -import { useState } from 'react' +import { useState, useEffect } from 'react' import { useRouter } from 'next/navigation' +import { useSession } from 'next-auth/react' import { Button } from '@/components/ui/Button' import { Input } from '@/components/ui/Input' import Link from 'next/link' @@ -18,6 +19,14 @@ export default function RegisterPage() { const [error, setError] = useState('') const [isLoading, setIsLoading] = useState(false) const router = useRouter() + const { data: session, status } = useSession() + + // Redirection si l'utilisateur est déjà connecté + useEffect(() => { + if (status === 'authenticated' && session) { + router.push('/') + } + }, [status, session, router]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() @@ -67,6 +76,23 @@ export default function RegisterPage() { } } + // Afficher un loader pendant la vérification de session + if (status === 'loading') { + return ( +
+ +
+
Chargement...
+
+
+ ) + } + + // Ne pas afficher le formulaire si l'utilisateur est connecté + if (status === 'authenticated') { + return null + } + return (