- 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
108 lines
3.3 KiB
Dart
108 lines
3.3 KiB
Dart
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:google_sign_in/google_sign_in.dart';
|
|
import 'package:google_sign_in_platform_interface/google_sign_in_platform_interface.dart';
|
|
|
|
class AuthService {
|
|
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
|
|
|
|
User? get currentUser => firebaseAuth.currentUser;
|
|
|
|
Stream<User?> get authStateChanges => firebaseAuth.authStateChanges();
|
|
|
|
Future<UserCredential> signInWithEmailAndPassword({
|
|
required String email,
|
|
required String password
|
|
}) async {
|
|
return await firebaseAuth.signInWithEmailAndPassword(
|
|
email: email, password: password);
|
|
}
|
|
|
|
Future<UserCredential> signUpWithEmailAndPassword({
|
|
required String email,
|
|
required String password
|
|
}) async {
|
|
return await firebaseAuth.createUserWithEmailAndPassword(
|
|
email: email, password: password);
|
|
}
|
|
|
|
Future<void> signOut() async {
|
|
await firebaseAuth.signOut();
|
|
}
|
|
|
|
Future<void> resetPassword(String email) async {
|
|
await firebaseAuth.sendPasswordResetEmail(email: email);
|
|
}
|
|
|
|
Future<void> updateDisplayName({
|
|
required String displayName,
|
|
}) async {
|
|
await currentUser!.updateDisplayName(displayName);
|
|
}
|
|
|
|
Future<void> deleteAccount({
|
|
required String password,
|
|
required String email,
|
|
}) async {
|
|
// Re-authenticate the user
|
|
AuthCredential credential =
|
|
EmailAuthProvider.credential(email: email, password: password);
|
|
await currentUser!.reauthenticateWithCredential(credential);
|
|
|
|
// Delete the user
|
|
await currentUser!.delete();
|
|
await firebaseAuth.signOut();
|
|
}
|
|
|
|
Future<void> resetPasswordFromCurrentPassword({
|
|
required String currentPassword,
|
|
required String newPassword,
|
|
required String email,
|
|
}) async {
|
|
// Re-authenticate the user
|
|
AuthCredential credential =
|
|
EmailAuthProvider.credential(email: email, password: currentPassword);
|
|
await currentUser!.reauthenticateWithCredential(credential);
|
|
|
|
// Update the password
|
|
await currentUser!.updatePassword(newPassword);
|
|
}
|
|
|
|
Future<void> ensureInitialized(){
|
|
return GoogleSignInPlatform.instance.init(const InitParameters());
|
|
}
|
|
|
|
Future<UserCredential> signInWithGoogle() async {
|
|
try {
|
|
await ensureInitialized();
|
|
final AuthenticationResults result = await GoogleSignInPlatform.instance.authenticate(const AuthenticateParameters());
|
|
final String? idToken = result.authenticationTokens.idToken;
|
|
if (idToken == null) {
|
|
throw FirebaseAuthException(
|
|
code: 'ERROR_MISSING_GOOGLE_ID_TOKEN',
|
|
message: 'Missing Google ID Token',
|
|
);
|
|
} else {
|
|
final OAuthCredential credential = GoogleAuthProvider.credential(idToken: idToken);
|
|
UserCredential userCredential = await firebaseAuth.signInWithCredential(credential);
|
|
|
|
// Retourner le UserCredential au lieu de void
|
|
return userCredential;
|
|
}
|
|
|
|
} on GoogleSignInException catch (e) {
|
|
print('Erreur lors de l\'initialisation de Google Sign-In: $e');
|
|
rethrow;
|
|
} on FirebaseAuthException catch (e) {
|
|
print('Erreur Firebase lors de l\'initialisation de Google Sign-In: $e');
|
|
rethrow;
|
|
} catch (e) {
|
|
print('Erreur inconnue lors de l\'initialisation de Google Sign-In: $e');
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future signInWithApple() async {
|
|
// TODO: Implémenter la connexion avec Apple
|
|
}
|
|
|
|
} |