Files
WalletTracker/src/services/authService.ts
Arthur Lempereur 8bde3d4f21 Initial commit: WalletTracker app with Firebase integration
- Setup Expo project with TypeScript
- Implement authentication (Login/Signup/Logout)
- Create Dashboard, Transactions, Subscriptions, and Analysis screens
- Add Firebase services (Auth, Firestore, Storage)
- Implement real-time synchronization
- Add charts and analytics
- Create reusable components (Button, InputText, TransactionCard, SubscriptionCard)
- Configure React Navigation with bottom tabs
- Add Firestore security rules
- Create comprehensive documentation (README, FIREBASE_SETUP, TESTING)
2025-10-23 14:36:36 +02:00

97 lines
2.9 KiB
TypeScript

import {
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
signOut,
User as FirebaseUser,
updateProfile
} from 'firebase/auth';
import { doc, setDoc, getDoc } from 'firebase/firestore';
import { auth, db } from '../config/firebase';
import { User } from '../types';
export const authService = {
// Inscription
async signup(email: string, password: string, displayName?: string): Promise<FirebaseUser> {
try {
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
const user = userCredential.user;
// Mettre à jour le profil avec le nom d'affichage
if (displayName) {
await updateProfile(user, { displayName });
}
// Créer le document utilisateur dans Firestore
const userData: Omit<User, 'uid'> = {
email: user.email!,
displayName: displayName || undefined,
createdAt: new Date(),
sharedWith: []
};
await setDoc(doc(db, 'users', user.uid), userData);
return user;
} catch (error: any) {
throw new Error(this.getErrorMessage(error.code));
}
},
// Connexion
async login(email: string, password: string): Promise<FirebaseUser> {
try {
const userCredential = await signInWithEmailAndPassword(auth, email, password);
return userCredential.user;
} catch (error: any) {
throw new Error(this.getErrorMessage(error.code));
}
},
// Déconnexion
async logout(): Promise<void> {
try {
await signOut(auth);
} catch (error: any) {
throw new Error('Erreur lors de la déconnexion');
}
},
// Récupérer les données utilisateur depuis Firestore
async getUserData(uid: string): Promise<User | null> {
try {
const userDoc = await getDoc(doc(db, 'users', uid));
if (userDoc.exists()) {
return { uid, ...userDoc.data() } as User;
}
return null;
} catch (error) {
console.error('Erreur lors de la récupération des données utilisateur:', error);
return null;
}
},
// Messages d'erreur en français
getErrorMessage(errorCode: string): string {
switch (errorCode) {
case 'auth/email-already-in-use':
return 'Cette adresse email est déjà utilisée';
case 'auth/invalid-email':
return 'Adresse email invalide';
case 'auth/operation-not-allowed':
return 'Opération non autorisée';
case 'auth/weak-password':
return 'Le mot de passe doit contenir au moins 6 caractères';
case 'auth/user-disabled':
return 'Ce compte a été désactivé';
case 'auth/user-not-found':
return 'Aucun compte trouvé avec cette adresse email';
case 'auth/wrong-password':
return 'Mot de passe incorrect';
case 'auth/invalid-credential':
return 'Identifiants invalides';
default:
return 'Une erreur est survenue. Veuillez réessayer.';
}
}
};