Skip to content

Commit 0716294

Browse files
Fix: Slow loading times after security rules update
Improved Firebase initialization and data fetching to address slow loading times caused by recent security rule changes.
1 parent 11eced0 commit 0716294

File tree

1 file changed

+7
-26
lines changed

1 file changed

+7
-26
lines changed

src/hooks/useAuth.tsx

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,29 @@ import { auth, googleProvider } from '@/lib/firebase';
1212

1313
export const useAuth = () => {
1414
const [user, setUser] = useState<User | null>(null);
15-
const [loading, setLoading] = useState(true); // Commence à true pour éviter les flickers
16-
const [initialized, setInitialized] = useState(false);
17-
const unsubscribeRef = useRef<(() => void) | null>(null);
15+
const [loading, setLoading] = useState(true);
16+
const hasInitialized = useRef(false);
1817

1918
useEffect(() => {
20-
// Éviter les initialisations multiples
21-
if (initialized) {
19+
// Empêcher les initialisations multiples
20+
if (hasInitialized.current) {
2221
return;
2322
}
2423

2524
console.log('🔐 Initialisation de l\'authentification...');
26-
27-
// S'assurer qu'on n'a qu'un seul listener
28-
if (unsubscribeRef.current) {
29-
unsubscribeRef.current();
30-
}
25+
hasInitialized.current = true;
3126

3227
const unsubscribe = onAuthStateChanged(auth, (user) => {
3328
console.log('🔐 État d\'authentification changé:', user ? 'Connecté' : 'Déconnecté');
3429
setUser(user);
3530
setLoading(false);
36-
if (!initialized) {
37-
setInitialized(true);
38-
}
3931
}, (error) => {
4032
console.error('🚨 Erreur d\'authentification:', error);
4133
setLoading(false);
42-
if (!initialized) {
43-
setInitialized(true);
44-
}
4534
});
4635

47-
unsubscribeRef.current = unsubscribe;
48-
49-
return () => {
50-
if (unsubscribeRef.current) {
51-
unsubscribeRef.current();
52-
unsubscribeRef.current = null;
53-
}
54-
};
55-
}, [initialized]); // Dépendance sur initialized pour éviter les boucles
36+
return unsubscribe;
37+
}, []); // Pas de dépendances !
5638

5739
const signInWithEmail = async (email: string, password: string) => {
5840
console.log('🔐 Tentative de connexion avec email:', email);
@@ -113,7 +95,6 @@ export const useAuth = () => {
11395
return {
11496
user,
11597
loading,
116-
initialized,
11798
signInWithEmail,
11899
signUpWithEmail,
119100
signInWithGoogle,

0 commit comments

Comments
 (0)