Files
phone_login/lib/services/auth_service.dart
soragui c903430f75 refactor: implement Flutter best practices and proper architecture
- Create proper service layer with AuthService and FirebaseAuthService
- Implement UserModel for proper data representation
- Enhance AuthState with proper loading states and error handling
- Convert stateless widgets to stateful where appropriate
- Add proper form validation and user feedback mechanisms
- Implement comprehensive error handling and loading indicators
- Fix redirect logic in router for proper authentication flow
- Create theme system with light and dark themes
- Add shared components like LoadingIndicator
- Improve code organization following recommended architecture
- Add proper disposal of controllers and focus nodes
- Implement proper null safety handling

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-26 06:35:57 +08:00

47 lines
1.4 KiB
Dart

import 'package:firebase_auth/firebase_auth.dart';
abstract class AuthService {
Stream<User?> get user;
Future<void> signInWithPhoneNumber(String phoneNumber);
Future<void> verifyOTP(String verificationId, String otp);
Future<void> signOut();
}
class FirebaseAuthService implements AuthService {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
@override
Stream<User?> get user => _firebaseAuth.authStateChanges();
@override
Future<void> 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<void> verifyOTP(String verificationId, String otp) async {
PhoneAuthCredential credential = PhoneAuthProvider.credential(
verificationId: verificationId,
smsCode: otp,
);
await _firebaseAuth.signInWithCredential(credential);
}
@override
Future<void> signOut() async {
await _firebaseAuth.signOut();
}
}