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