Refactor signup page to use BLoC pattern and implement authentication repository
- Updated signup.dart to replace Provider with BLoC for state management. - Created AuthRepository to handle authentication logic and Firestore user management. - Added TripRepository and UserRepository for trip and user data management. - Implemented methods for user sign-in, sign-up, and data retrieval in repositories. - Enhanced trip management with create, update, delete, and participant management functionalities. - Updated AuthService to include new methods for sign-in and sign-up. - Removed unnecessary print statements from TripService for cleaner code. - Added dependencies for flutter_bloc and equatable in pubspec.yaml. Not tested yet
This commit is contained in:
148
lib/repositories/auth_repository.dart
Normal file
148
lib/repositories/auth_repository.dart
Normal file
@@ -0,0 +1,148 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart' as firebase_auth;
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import '../data/models/user.dart';
|
||||
import '../services/auth_service.dart';
|
||||
|
||||
class AuthRepository {
|
||||
final AuthService _authService;
|
||||
final FirebaseFirestore _firestore;
|
||||
|
||||
AuthRepository({
|
||||
AuthService? authService,
|
||||
FirebaseFirestore? firestore,
|
||||
}) : _authService = authService ?? AuthService(),
|
||||
_firestore = firestore ?? FirebaseFirestore.instance;
|
||||
|
||||
// Vérifier l'état de connexion actuel
|
||||
Stream<firebase_auth.User?> get authStateChanges =>
|
||||
_authService.authStateChanges;
|
||||
|
||||
firebase_auth.User? get currentUser => _authService.currentUser;
|
||||
|
||||
// Connexion avec email/mot de passe
|
||||
Future<User?> signInWithEmailAndPassword({
|
||||
required String email,
|
||||
required String password,
|
||||
}) async {
|
||||
try {
|
||||
final firebaseUser = await _authService.signInWithEmailAndPassword(
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
return await getUserFromFirestore(firebaseUser.user!.uid);
|
||||
} catch (e) {
|
||||
throw Exception('Erreur de connexion: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Inscription avec email/mot de passe
|
||||
Future<User?> signUpWithEmailAndPassword({
|
||||
required String email,
|
||||
required String password,
|
||||
required String nom,
|
||||
required String prenom,
|
||||
}) async {
|
||||
try {
|
||||
final firebaseUser = await _authService.signUpWithEmailAndPassword(
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
|
||||
// Créer le document utilisateur dans Firestore
|
||||
final user = User(
|
||||
id: firebaseUser.user!.uid,
|
||||
email: email,
|
||||
nom: nom,
|
||||
prenom: prenom,
|
||||
);
|
||||
|
||||
await _firestore.collection('users').doc(user.id).set(user.toMap());
|
||||
return user;
|
||||
|
||||
} catch (e) {
|
||||
throw Exception('Erreur d\'inscription: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Connexion avec Google
|
||||
Future<User?> signInWithGoogle() async {
|
||||
try {
|
||||
final firebaseUser = await _authService.signInWithGoogle();
|
||||
|
||||
if (firebaseUser.user != null) {
|
||||
// Vérifier si l'utilisateur existe déjà
|
||||
final existingUser = await getUserFromFirestore(firebaseUser.user!.uid);
|
||||
|
||||
if (existingUser != null) {
|
||||
return existingUser;
|
||||
}
|
||||
|
||||
// Créer un nouvel utilisateur
|
||||
final user = User(
|
||||
id: firebaseUser.user!.uid,
|
||||
email: firebaseUser.user!.email ?? '',
|
||||
nom: '',
|
||||
prenom: firebaseUser.user!.displayName ?? 'Utilisateur',
|
||||
);
|
||||
|
||||
await _firestore.collection('users').doc(user.id).set(user.toMap());
|
||||
return user;
|
||||
}
|
||||
return null;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur de connexion Google: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Connexion avec Apple
|
||||
Future<User?> signInWithApple() async {
|
||||
try {
|
||||
final firebaseUser = await _authService.signInWithApple();
|
||||
|
||||
if (firebaseUser?.user != null) {
|
||||
final existingUser = await getUserFromFirestore(firebaseUser!.user!.uid);
|
||||
|
||||
if (existingUser != null) {
|
||||
return existingUser;
|
||||
}
|
||||
|
||||
final user = User(
|
||||
id: firebaseUser.user!.uid,
|
||||
email: firebaseUser.user!.email ?? '',
|
||||
nom: '',
|
||||
prenom: firebaseUser.user!.displayName ?? 'Utilisateur',
|
||||
);
|
||||
|
||||
await _firestore.collection('users').doc(user.id).set(user.toMap());
|
||||
return user;
|
||||
}
|
||||
return null;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur de connexion Apple: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Déconnexion
|
||||
Future<void> signOut() async {
|
||||
await _authService.signOut();
|
||||
}
|
||||
|
||||
// Réinitialisation du mot de passe
|
||||
Future<void> resetPassword(String email) async {
|
||||
await _authService.resetPassword(email);
|
||||
}
|
||||
|
||||
// Récupérer les données utilisateur depuis Firestore
|
||||
Future<User?> getUserFromFirestore(String uid) async {
|
||||
try {
|
||||
final doc = await _firestore.collection('users').doc(uid).get();
|
||||
if (doc.exists) {
|
||||
final data = doc.data() as Map<String, dynamic>;
|
||||
return User.fromMap({...data, 'id': uid});
|
||||
}
|
||||
return null;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
113
lib/repositories/trip_repository.dart
Normal file
113
lib/repositories/trip_repository.dart
Normal file
@@ -0,0 +1,113 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import '../data/models/trip.dart';
|
||||
|
||||
class TripRepository {
|
||||
final FirebaseFirestore _firestore;
|
||||
|
||||
TripRepository({FirebaseFirestore? firestore})
|
||||
: _firestore = firestore ?? FirebaseFirestore.instance;
|
||||
|
||||
// Créer un voyage
|
||||
Future<Trip> createTrip(Trip trip) async {
|
||||
try {
|
||||
final docRef = await _firestore.collection('trips').add(trip.toMap());
|
||||
final createdTrip = trip.copyWith(id: docRef.id);
|
||||
|
||||
// Mettre à jour avec l'ID généré
|
||||
await docRef.update({'id': docRef.id});
|
||||
|
||||
return createdTrip;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la création du voyage: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Récupérer les voyages d'un utilisateur
|
||||
Stream<List<Trip>> getUserTrips(String userId) {
|
||||
return _firestore
|
||||
.collection('trips')
|
||||
.where('createdBy', isEqualTo: userId)
|
||||
.snapshots()
|
||||
.map((snapshot) {
|
||||
return snapshot.docs.map((doc) {
|
||||
final data = doc.data();
|
||||
return Trip.fromMap({...data, 'id': doc.id});
|
||||
}).toList();
|
||||
});
|
||||
}
|
||||
|
||||
// Récupérer les voyages où l'utilisateur est participant
|
||||
Stream<List<Trip>> getSharedTrips(String userId) {
|
||||
return _firestore
|
||||
.collection('trips')
|
||||
.where('participants', arrayContains: userId)
|
||||
.snapshots()
|
||||
.map((snapshot) {
|
||||
return snapshot.docs.map((doc) {
|
||||
final data = doc.data();
|
||||
return Trip.fromMap({...data, 'id': doc.id});
|
||||
}).toList();
|
||||
});
|
||||
}
|
||||
|
||||
// Récupérer un voyage par ID
|
||||
Future<Trip?> getTripById(String tripId) async {
|
||||
try {
|
||||
final doc = await _firestore.collection('trips').doc(tripId).get();
|
||||
if (doc.exists) {
|
||||
final data = doc.data() as Map<String, dynamic>;
|
||||
return Trip.fromMap({...data, 'id': doc.id});
|
||||
}
|
||||
return null;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération du voyage: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Mettre à jour un voyage
|
||||
Future<bool> updateTrip(Trip trip) async {
|
||||
try {
|
||||
await _firestore
|
||||
.collection('trips')
|
||||
.doc(trip.id)
|
||||
.update(trip.toMap());
|
||||
return true;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la mise à jour du voyage: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Supprimer un voyage
|
||||
Future<bool> deleteTrip(String tripId) async {
|
||||
try {
|
||||
await _firestore.collection('trips').doc(tripId).delete();
|
||||
return true;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression du voyage: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Ajouter un participant
|
||||
Future<bool> addParticipant(String tripId, String participantEmail) async {
|
||||
try {
|
||||
await _firestore.collection('trips').doc(tripId).update({
|
||||
'participants': FieldValue.arrayUnion([participantEmail])
|
||||
});
|
||||
return true;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de l\'ajout du participant: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Retirer un participant
|
||||
Future<bool> removeParticipant(String tripId, String participantEmail) async {
|
||||
try {
|
||||
await _firestore.collection('trips').doc(tripId).update({
|
||||
'participants': FieldValue.arrayRemove([participantEmail])
|
||||
});
|
||||
return true;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors du retrait du participant: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
95
lib/repositories/user_repository.dart
Normal file
95
lib/repositories/user_repository.dart
Normal file
@@ -0,0 +1,95 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import '../data/models/user.dart';
|
||||
import '../services/auth_service.dart';
|
||||
|
||||
class UserRepository {
|
||||
final FirebaseFirestore _firestore;
|
||||
final AuthService _authService;
|
||||
|
||||
UserRepository({
|
||||
FirebaseFirestore? firestore,
|
||||
AuthService? authService,
|
||||
}) : _firestore = firestore ?? FirebaseFirestore.instance,
|
||||
_authService = authService ?? AuthService();
|
||||
|
||||
// Récupérer un utilisateur par ID
|
||||
Future<User?> getUserById(String uid) async {
|
||||
try {
|
||||
final doc = await _firestore.collection('users').doc(uid).get();
|
||||
if (doc.exists) {
|
||||
final data = doc.data() as Map<String, dynamic>;
|
||||
return User.fromMap({...data, 'id': uid});
|
||||
}
|
||||
return null;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération de l\'utilisateur: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Récupérer un utilisateur par email
|
||||
Future<User?> getUserByEmail(String email) async {
|
||||
try {
|
||||
final querySnapshot = await _firestore
|
||||
.collection('users')
|
||||
.where('email', isEqualTo: email.trim())
|
||||
.limit(1)
|
||||
.get();
|
||||
|
||||
if (querySnapshot.docs.isNotEmpty) {
|
||||
final doc = querySnapshot.docs.first;
|
||||
final data = doc.data();
|
||||
return User.fromMap({...data, 'id': doc.id});
|
||||
}
|
||||
return null;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la recherche de l\'utilisateur: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Mettre à jour un utilisateur
|
||||
Future<bool> updateUser(User user) async {
|
||||
try {
|
||||
await _firestore.collection('users').doc(user.id).update(user.toMap());
|
||||
|
||||
// Mettre à jour le displayName dans Firebase Auth
|
||||
await _authService.updateDisplayName(displayName: user.fullName);
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la mise à jour: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Supprimer un utilisateur
|
||||
Future<bool> deleteUser(String uid) async {
|
||||
try {
|
||||
await _firestore.collection('users').doc(uid).delete();
|
||||
// Note: Vous devrez également supprimer le compte Firebase Auth
|
||||
return true;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Changer le mot de passe
|
||||
Future<bool> changePassword({
|
||||
required String currentPassword,
|
||||
required String newPassword,
|
||||
}) async {
|
||||
try {
|
||||
final currentUser = _authService.currentUser;
|
||||
if (currentUser?.email == null) {
|
||||
throw Exception('Utilisateur non connecté ou email non disponible');
|
||||
}
|
||||
|
||||
await _authService.resetPasswordFromCurrentPassword(
|
||||
email: currentUser!.email!,
|
||||
currentPassword: currentPassword,
|
||||
newPassword: newPassword,
|
||||
);
|
||||
return true;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors du changement de mot de passe: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user