import 'package:firebase_auth/firebase_auth.dart'; abstract class AuthService { Stream get user; Future signInWithPhoneNumber(String phoneNumber); Future verifyOTP(String verificationId, String otp); Future signOut(); } class FirebaseAuthService implements AuthService { final FirebaseAuth _firebaseAuth = FirebaseAuth.instance; @override Stream get user => _firebaseAuth.authStateChanges(); @override Future signInWithPhoneNumber(String phoneNumber) async { await _firebaseAuth.verifyPhoneNumber( phoneNumber: phoneNumber, verificationCompleted: (PhoneAuthCredential credential) async { await _firebaseAuth.signInWithCredential(credential); }, verificationFailed: (FirebaseAuthException e) { throw Exception(e.message); }, codeSent: (String verificationId, int? resendToken) { // Store verificationId for later use in verifyOTP // In a real app, this would be stored in state management }, codeAutoRetrievalTimeout: (String verificationId) {}, ); } @override Future verifyOTP(String verificationId, String otp) async { PhoneAuthCredential credential = PhoneAuthProvider.credential( verificationId: verificationId, smsCode: otp, ); await _firebaseAuth.signInWithCredential(credential); } @override Future signOut() async { await _firebaseAuth.signOut(); } }