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 get authStateChanges => firebaseAuth.authStateChanges(); Future signIn({ required String email, required String password }) async { return await firebaseAuth.signInWithEmailAndPassword( email: email, password: password); } Future createAccount({ required String email, required String password }) async { return await firebaseAuth.createUserWithEmailAndPassword( email: email, password: password); } Future signOut() async { await firebaseAuth.signOut(); } Future resetPassword(String email) async { await firebaseAuth.sendPasswordResetEmail(email: email); } Future updateDisplayName({ required String displayName, }) async { await currentUser!.updateDisplayName(displayName); } Future 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 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 ensureInitialized(){ return GoogleSignInPlatform.instance.init(const InitParameters()); } Future 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; } } }