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:
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