Skip to content

Commit a181113

Browse files
Fix: Authentication issues after Firestore rules update
Addressed continued authentication problems after Firestore rules update. Identified and addressed potential causes, including Firebase configuration and environment variables. Provided guidance on troubleshooting steps.
1 parent 220a90d commit a181113

2 files changed

Lines changed: 33 additions & 10 deletions

File tree

src/hooks/useAuth.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ export const useAuth = () => {
1919
const { executeWithRetry } = useRetry({ maxRetries: 2 });
2020

2121
useEffect(() => {
22+
console.log('🔐 Initialisation de l\'authentification...');
23+
2224
const unsubscribe = onAuthStateChanged(auth, (user) => {
25+
console.log('🔐 État d\'authentification changé:', user ? 'Connecté' : 'Déconnecté');
2326
setUser(user);
2427
setLoading(false);
2528
}, (error) => {
29+
console.error('🚨 Erreur d\'authentification:', error);
2630
handleError(error, {
2731
showToast: true,
2832
logError: true
@@ -34,45 +38,57 @@ export const useAuth = () => {
3438
}, [handleError]);
3539

3640
const signInWithEmail = async (email: string, password: string) => {
41+
console.log('🔐 Tentative de connexion avec email:', email);
3742
try {
38-
await executeWithRetry(
43+
const result = await executeWithRetry(
3944
() => signInWithEmailAndPassword(auth, email, password),
4045
{ component: 'useAuth', action: 'signInWithEmail' }
4146
);
47+
console.log('✅ Connexion réussie:', result.user.uid);
4248
} catch (error) {
43-
throw error; // L'erreur a déjà été gérée par executeWithRetry
49+
console.error('❌ Erreur de connexion:', error);
50+
throw error;
4451
}
4552
};
4653

4754
const signUpWithEmail = async (email: string, password: string) => {
55+
console.log('🔐 Tentative de création de compte avec email:', email);
4856
try {
49-
await executeWithRetry(
57+
const result = await executeWithRetry(
5058
() => createUserWithEmailAndPassword(auth, email, password),
5159
{ component: 'useAuth', action: 'signUpWithEmail' }
5260
);
61+
console.log('✅ Compte créé:', result.user.uid);
5362
} catch (error) {
63+
console.error('❌ Erreur de création de compte:', error);
5464
throw error;
5565
}
5666
};
5767

5868
const signInWithGoogle = async () => {
69+
console.log('🔐 Tentative de connexion avec Google');
5970
try {
60-
await executeWithRetry(
71+
const result = await executeWithRetry(
6172
() => signInWithPopup(auth, googleProvider),
6273
{ component: 'useAuth', action: 'signInWithGoogle' }
6374
);
75+
console.log('✅ Connexion Google réussie:', result.user.uid);
6476
} catch (error) {
77+
console.error('❌ Erreur de connexion Google:', error);
6578
throw error;
6679
}
6780
};
6881

6982
const logout = async () => {
83+
console.log('🔐 Tentative de déconnexion');
7084
try {
7185
await executeWithRetry(
7286
() => signOut(auth),
7387
{ component: 'useAuth', action: 'logout' }
7488
);
89+
console.log('✅ Déconnexion réussie');
7590
} catch (error) {
91+
console.error('❌ Erreur de déconnexion:', error);
7692
throw error;
7793
}
7894
};

src/lib/firebase.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { initializeApp } from "firebase/app";
33
import { getAuth, GoogleAuthProvider } from "firebase/auth";
44
import { getFirestore } from "firebase/firestore";
5-
import { getAnalytics } from "firebase/analytics";
5+
import { getAnalytics, isSupported } from "firebase/analytics";
66

77
const firebaseConfig = {
88
apiKey: import.meta.env.VITE_FIREBASE_API_KEY || "AIzaSyAlHsC-w7Sx18XKJ6dIcxvqj-AUdqkjqSE",
@@ -37,10 +37,17 @@ googleProvider.setCustomParameters({
3737
prompt: 'select_account'
3838
});
3939

40-
// Analytics (optional, only if in browser and measurement ID provided)
41-
export const analytics =
42-
typeof window !== 'undefined' && firebaseConfig.measurementId
43-
? getAnalytics(app)
44-
: null;
40+
// Analytics avec vérification de support
41+
export const analytics = await (async () => {
42+
try {
43+
if (typeof window !== 'undefined' && firebaseConfig.measurementId && await isSupported()) {
44+
return getAnalytics(app);
45+
}
46+
return null;
47+
} catch (error) {
48+
console.warn('Analytics non disponible dans cet environnement');
49+
return null;
50+
}
51+
})();
4552

4653
export default app;

0 commit comments

Comments
 (0)