refactor: simplify FirebaseAuth and dataConnect usage for clarity and consistency

This commit is contained in:
Achintha Isuru
2026-01-24 10:35:20 -05:00
parent 2e54014501
commit 084ef8b025

View File

@@ -1,27 +1,26 @@
import 'dart:async'; import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart' as firebase; import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_data_connect/firebase_data_connect.dart'; import 'package:firebase_data_connect/firebase_data_connect.dart';
import 'package:krow_data_connect/krow_data_connect.dart' as dc; import 'package:krow_data_connect/krow_data_connect.dart';
import 'package:krow_domain/krow_domain.dart' as domain; import 'package:krow_domain/krow_domain.dart' as domain;
import '../../domain/repositories/auth_repository_interface.dart'; import '../../domain/repositories/auth_repository_interface.dart';
/// Implementation of [AuthRepositoryInterface]. /// Implementation of [AuthRepositoryInterface].
class AuthRepositoryImpl implements AuthRepositoryInterface { class AuthRepositoryImpl implements AuthRepositoryInterface {
final firebase.FirebaseAuth _firebaseAuth;
final dc.ExampleConnector _dataConnect;
AuthRepositoryImpl({ AuthRepositoryImpl({
required firebase.FirebaseAuth firebaseAuth, required this.firebaseAuth,
required dc.ExampleConnector dataConnect, required this.dataConnect,
}) : _firebaseAuth = firebaseAuth, });
_dataConnect = dataConnect;
final FirebaseAuth firebaseAuth;
final ExampleConnector dataConnect;
@override @override
Stream<domain.User?> get currentUser => _firebaseAuth Stream<domain.User?> get currentUser => firebaseAuth
.authStateChanges() .authStateChanges()
.map((firebase.User? firebaseUser) { .map((User? firebaseUser) {
if (firebaseUser == null) { if (firebaseUser == null) {
return null; return null;
} }
@@ -39,11 +38,17 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
Future<String?> signInWithPhone({required String phoneNumber}) async { Future<String?> signInWithPhone({required String phoneNumber}) async {
final Completer<String?> completer = Completer<String?>(); final Completer<String?> completer = Completer<String?>();
await _firebaseAuth.verifyPhoneNumber( print('Starting phone number verification for $phoneNumber');
await firebaseAuth.verifyPhoneNumber(
phoneNumber: phoneNumber, phoneNumber: phoneNumber,
verificationCompleted: (_) {}, verificationCompleted: (_) {
verificationFailed: (firebase.FirebaseAuthException e) { print('Phone verification completed automatically.');
print(phoneNumber);
},
verificationFailed: (FirebaseAuthException e) {
if (!completer.isCompleted) { if (!completer.isCompleted) {
print('Phone verification failed: ${e.message}');
completer.completeError( completer.completeError(
Exception(e.message ?? 'Phone verification failed.'), Exception(e.message ?? 'Phone verification failed.'),
); );
@@ -67,7 +72,7 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
/// Signs out the current user. /// Signs out the current user.
@override @override
Future<void> signOut() { Future<void> signOut() {
return _firebaseAuth.signOut(); return firebaseAuth.signOut();
} }
/// Verifies an OTP code and returns the authenticated user. /// Verifies an OTP code and returns the authenticated user.
@@ -76,32 +81,32 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
required String verificationId, required String verificationId,
required String smsCode, required String smsCode,
}) async { }) async {
final firebase.PhoneAuthCredential credential = firebase.PhoneAuthProvider.credential( final PhoneAuthCredential credential = PhoneAuthProvider.credential(
verificationId: verificationId, verificationId: verificationId,
smsCode: smsCode, smsCode: smsCode,
); );
final firebase.UserCredential userCredential = await _firebaseAuth.signInWithCredential(credential); final UserCredential userCredential = await firebaseAuth.signInWithCredential(credential);
final firebase.User? firebaseUser = userCredential.user; final User? firebaseUser = userCredential.user;
if (firebaseUser == null) { if (firebaseUser == null) {
throw Exception('Phone verification failed, no Firebase user received.'); throw Exception('Phone verification failed, no Firebase user received.');
} }
final QueryResult<dc.GetUserByIdData, dc.GetUserByIdVariables> response = await _dataConnect.getUserById( final QueryResult<GetUserByIdData, GetUserByIdVariables> response = await dataConnect.getUserById(
id: firebaseUser.uid, id: firebaseUser.uid,
).execute(); ).execute();
final dc.GetUserByIdUser? user = response.data?.user; final GetUserByIdUser? user = response.data.user;
if (user == null) { if (user == null) {
await _firebaseAuth.signOut(); await firebaseAuth.signOut();
throw Exception('Authenticated user profile not found in database.'); throw Exception('Authenticated user profile not found in database.');
} }
if (user.userRole != 'STAFF') { if (user.userRole != 'STAFF') {
await _firebaseAuth.signOut(); await firebaseAuth.signOut();
throw Exception('User is not authorized for this app.'); throw Exception('User is not authorized for this app.');
} }
final String email = user.email ?? ''; final String email = user.email ?? '';
if (email.isEmpty) { if (email.isEmpty) {
await _firebaseAuth.signOut(); await firebaseAuth.signOut();
throw Exception('User email is missing in profile data.'); throw Exception('User email is missing in profile data.');
} }