@@ -14,31 +14,43 @@ import { useRetry } from './useRetry';
1414
1515export const useAuth = ( ) => {
1616 const [ user , setUser ] = useState < User | null > ( null ) ;
17- const [ loading , setLoading ] = useState ( true ) ;
17+ const [ loading , setLoading ] = useState ( false ) ; // Changé de true à false pour affichage immédiat
18+ const [ initialized , setInitialized ] = useState ( false ) ;
1819 const { handleError } = useErrorHandler ( ) ;
1920 const { executeWithRetry } = useRetry ( { maxRetries : 2 } ) ;
2021
2122 useEffect ( ( ) => {
2223 console . log ( '🔐 Initialisation de l\'authentification...' ) ;
2324
24- const unsubscribe = onAuthStateChanged ( auth , ( user ) => {
25- console . log ( '🔐 État d\'authentification changé:' , user ? 'Connecté' : 'Déconnecté' ) ;
26- setUser ( user ) ;
27- setLoading ( false ) ;
28- } , ( error ) => {
29- console . error ( '🚨 Erreur d\'authentification:' , error ) ;
30- handleError ( error , {
31- showToast : true ,
32- logError : true
25+ // Délai très court pour permettre l'affichage de l'UI
26+ const initTimer = setTimeout ( ( ) => {
27+ const unsubscribe = onAuthStateChanged ( auth , ( user ) => {
28+ console . log ( '🔐 État d\'authentification changé:' , user ? 'Connecté' : 'Déconnecté' ) ;
29+ setUser ( user ) ;
30+ setLoading ( false ) ;
31+ setInitialized ( true ) ;
32+ } , ( error ) => {
33+ console . error ( '🚨 Erreur d\'authentification:' , error ) ;
34+ handleError ( error , {
35+ showToast : true ,
36+ logError : true
37+ } ) ;
38+ setLoading ( false ) ;
39+ setInitialized ( true ) ;
3340 } ) ;
34- setLoading ( false ) ;
35- } ) ;
3641
37- return unsubscribe ;
42+ return ( ) => {
43+ clearTimeout ( initTimer ) ;
44+ unsubscribe ( ) ;
45+ } ;
46+ } , 100 ) ; // Délai minimal pour l'affichage
47+
48+ return ( ) => clearTimeout ( initTimer ) ;
3849 } , [ handleError ] ) ;
3950
4051 const signInWithEmail = async ( email : string , password : string ) => {
4152 console . log ( '🔐 Tentative de connexion avec email:' , email ) ;
53+ setLoading ( true ) ;
4254 try {
4355 const result = await executeWithRetry (
4456 ( ) => signInWithEmailAndPassword ( auth , email , password ) ,
@@ -48,11 +60,14 @@ export const useAuth = () => {
4860 } catch ( error ) {
4961 console . error ( '❌ Erreur de connexion:' , error ) ;
5062 throw error ;
63+ } finally {
64+ setLoading ( false ) ;
5165 }
5266 } ;
5367
5468 const signUpWithEmail = async ( email : string , password : string ) => {
5569 console . log ( '🔐 Tentative de création de compte avec email:' , email ) ;
70+ setLoading ( true ) ;
5671 try {
5772 const result = await executeWithRetry (
5873 ( ) => createUserWithEmailAndPassword ( auth , email , password ) ,
@@ -62,11 +77,14 @@ export const useAuth = () => {
6277 } catch ( error ) {
6378 console . error ( '❌ Erreur de création de compte:' , error ) ;
6479 throw error ;
80+ } finally {
81+ setLoading ( false ) ;
6582 }
6683 } ;
6784
6885 const signInWithGoogle = async ( ) => {
6986 console . log ( '🔐 Tentative de connexion avec Google' ) ;
87+ setLoading ( true ) ;
7088 try {
7189 const result = await executeWithRetry (
7290 ( ) => signInWithPopup ( auth , googleProvider ) ,
@@ -76,11 +94,14 @@ export const useAuth = () => {
7694 } catch ( error ) {
7795 console . error ( '❌ Erreur de connexion Google:' , error ) ;
7896 throw error ;
97+ } finally {
98+ setLoading ( false ) ;
7999 }
80100 } ;
81101
82102 const logout = async ( ) => {
83103 console . log ( '🔐 Tentative de déconnexion' ) ;
104+ setLoading ( true ) ;
84105 try {
85106 await executeWithRetry (
86107 ( ) => signOut ( auth ) ,
@@ -90,12 +111,15 @@ export const useAuth = () => {
90111 } catch ( error ) {
91112 console . error ( '❌ Erreur de déconnexion:' , error ) ;
92113 throw error ;
114+ } finally {
115+ setLoading ( false ) ;
93116 }
94117 } ;
95118
96119 return {
97120 user,
98121 loading,
122+ initialized,
99123 signInWithEmail,
100124 signUpWithEmail,
101125 signInWithGoogle,
0 commit comments