chore: remove Firebase dependencies and legacy code (Fase 4)
- Delete all firebase_*_repository.dart files (replaced by api_* equivalents) - Remove cloud_firestore, firebase_auth, firebase_storage, firebase_core from all package pubspecs - Remove cloud_firestore from main pubspec.yaml - Delete firebase_options.dart (no longer referenced) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
733384091c
commit
af91e0ba04
@@ -1,421 +0,0 @@
|
||||
import 'dart:async';
|
||||
import 'dart:developer';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:firebase_storage/firebase_storage.dart';
|
||||
import 'package:user_repository/src/models/models.dart';
|
||||
import '../entities/entities.dart';
|
||||
import 'user_repo.dart';
|
||||
|
||||
class FirebaseUserRepository implements UserRepository {
|
||||
MyUser? _lastUser;
|
||||
|
||||
final FirebaseAuth _firebaseAuth;
|
||||
final usersCollection = FirebaseFirestore.instance.collection('users');
|
||||
final StreamController<MyUser?> _userStreamController =
|
||||
StreamController<MyUser?>.broadcast();
|
||||
String verificationId = '';
|
||||
|
||||
FirebaseUserRepository(this._firebaseAuth) {
|
||||
_firebaseAuth.userChanges().listen((user) async {
|
||||
if (user != null) {
|
||||
await updateFromFirebase2(
|
||||
userId: user.uid,
|
||||
email: user.email,
|
||||
name: user.displayName,
|
||||
phone: user.phoneNumber,
|
||||
picture: user.photoURL,
|
||||
nickname: user.displayName?.trim().toLowerCase(),
|
||||
);
|
||||
} else {
|
||||
_lastUser = null;
|
||||
_userStreamController.add(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<MyUser?> lastUser() async {
|
||||
return _lastUser;
|
||||
}
|
||||
|
||||
Future<void> refreshUser() async {
|
||||
final user = _firebaseAuth.currentUser;
|
||||
if (user != null) {
|
||||
await updateFromFirebase(user.uid);
|
||||
} else {
|
||||
_lastUser = null;
|
||||
_userStreamController.add(null);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateFromFirebase(String userId) async {
|
||||
return updateFromFirebase2(userId: userId);
|
||||
}
|
||||
|
||||
Future<void> updateFromFirebase2({
|
||||
required String userId,
|
||||
String? email,
|
||||
String? name,
|
||||
String? phone,
|
||||
String? picture,
|
||||
String? nickname,
|
||||
}) async {
|
||||
try {
|
||||
var myUser = await getMyUser(userId);
|
||||
|
||||
if (myUser == null) {
|
||||
await createUser(MyUser(
|
||||
id: userId,
|
||||
email: email,
|
||||
name: name,
|
||||
phone: phone,
|
||||
picture: picture,
|
||||
nickname: name?.trim().toLowerCase(),
|
||||
proState: ProState.inactive,
|
||||
));
|
||||
myUser = await getMyUser(userId);
|
||||
}
|
||||
_lastUser = myUser;
|
||||
_userStreamController.add(myUser);
|
||||
} catch (e) {
|
||||
log('xd -- Error updating from firebase ${e.toString()}');
|
||||
_lastUser = null;
|
||||
_userStreamController.add(null);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<MyUser?> streamUser() {
|
||||
return _userStreamController.stream;
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<bool> isAuthenticated() {
|
||||
return _firebaseAuth.userChanges().map((event) => null != event);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<MyUser> signUp(MyUser myUser, String password) async {
|
||||
try {
|
||||
UserCredential userCredential =
|
||||
await _firebaseAuth.createUserWithEmailAndPassword(
|
||||
email: myUser.email!,
|
||||
password: password,
|
||||
);
|
||||
|
||||
myUser = myUser.copyWith(id: userCredential.user!.uid);
|
||||
|
||||
return myUser;
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> signInWithPhoneNumber(String phoneNumber) async {
|
||||
try {
|
||||
await _firebaseAuth.verifyPhoneNumber(
|
||||
phoneNumber: phoneNumber,
|
||||
verificationCompleted: (PhoneAuthCredential credential) async {
|
||||
await _firebaseAuth.signInWithCredential(credential);
|
||||
},
|
||||
codeSent: (String verificationId, int? resendToken) {
|
||||
this.verificationId = verificationId;
|
||||
},
|
||||
codeAutoRetrievalTimeout: (String verificationId) {
|
||||
this.verificationId = verificationId;
|
||||
},
|
||||
verificationFailed: (FirebaseAuthException e) {
|
||||
if (e.code == 'invalid-phone-number') {
|
||||
} else if (e.code == 'network-request-failed') {
|
||||
} else {}
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> verifyOTP(String code) async {
|
||||
try {
|
||||
var credentials = await _firebaseAuth.signInWithCredential(
|
||||
PhoneAuthProvider.credential(
|
||||
verificationId: verificationId, smsCode: code));
|
||||
|
||||
if (credentials.user == null) {
|
||||
await setUserData(MyUser(
|
||||
id: credentials.user?.uid ?? '',
|
||||
phone: credentials.user?.phoneNumber ?? '',
|
||||
proState: ProState.inactive,
|
||||
));
|
||||
}
|
||||
|
||||
return credentials.user != null ? true : false;
|
||||
} catch (e) {
|
||||
if (e is FirebaseAuthException) {
|
||||
if (e.code == 'invalid-verification-code') {
|
||||
return false;
|
||||
} else {
|
||||
rethrow;
|
||||
}
|
||||
} else {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> signIn(String email, String password) async {
|
||||
try {
|
||||
await _firebaseAuth.signInWithEmailAndPassword(
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> addEmailAndPassword(String email, String password) async {
|
||||
try {
|
||||
await _firebaseAuth.currentUser!.updateEmail(email);
|
||||
await _firebaseAuth.currentUser!.updatePassword(password);
|
||||
|
||||
final user = await getMyUser(_firebaseAuth.currentUser!.uid);
|
||||
if (user == null) {
|
||||
return "user-not-found";
|
||||
}
|
||||
|
||||
final newUser = user.copyWith(email: email);
|
||||
await updateUserInfo(newUser);
|
||||
|
||||
return null;
|
||||
} catch (e) {
|
||||
if (e is FirebaseAuthException && e.code == 'requires-recent-login') {
|
||||
return 'requires-recent-login';
|
||||
}
|
||||
|
||||
if (e is FirebaseAuthException && e.code == 'email-already-in-use') {
|
||||
return 'email-already-in-use';
|
||||
}
|
||||
|
||||
return 'unknown-error';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
addPhoneAuthCredential(String password, String phoneNumber,
|
||||
{required Future<void> Function(Exception) verificationFailed,
|
||||
required Future<void> Function(String) codeSent,
|
||||
required Future<void> Function(String) codeAutoRetrievalTimeout}) async {
|
||||
await _firebaseAuth.verifyPhoneNumber(
|
||||
phoneNumber: phoneNumber,
|
||||
timeout: const Duration(seconds: 60),
|
||||
verificationCompleted: (AuthCredential authCredential) async {
|
||||
// La verificación se completó automáticamente.
|
||||
// TODO: Revisar si es necesario.
|
||||
},
|
||||
verificationFailed: (FirebaseAuthException authException) async {
|
||||
// La verificación falló.
|
||||
// throw authException;
|
||||
log('verificationFailed: $authException');
|
||||
await verificationFailed(authException);
|
||||
},
|
||||
codeAutoRetrievalTimeout: (String verificationId) async {
|
||||
// Tiempo de espera agotado para la recuperación automática del código.
|
||||
// throw 'timeout';
|
||||
log(verificationId);
|
||||
await codeAutoRetrievalTimeout(verificationId);
|
||||
},
|
||||
codeSent: (String verificationId, int? resendToken) async {
|
||||
await codeSent(verificationId);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> linkWithOTP(
|
||||
String phoneNumber, String verificationId, String code) async {
|
||||
try {
|
||||
var phoneAuthCredential = PhoneAuthProvider.credential(
|
||||
verificationId: verificationId, smsCode: code);
|
||||
|
||||
User? userAuth = FirebaseAuth.instance.currentUser;
|
||||
|
||||
if (userAuth == null) {
|
||||
throw 'User not found';
|
||||
}
|
||||
|
||||
final user = await getMyUser(_firebaseAuth.currentUser!.uid);
|
||||
if (user == null) {
|
||||
throw "user-not-found";
|
||||
}
|
||||
|
||||
await userAuth.linkWithCredential(phoneAuthCredential);
|
||||
final newUser = user.copyWith(phone: phoneNumber);
|
||||
await updateUserInfo(newUser);
|
||||
return true;
|
||||
} catch (e) {
|
||||
if (e is FirebaseAuthException) {
|
||||
if (e.code == 'invalid-verification-code') {
|
||||
return false;
|
||||
} else {
|
||||
rethrow;
|
||||
}
|
||||
} else {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<UpdatePassworErros?> updatePassword(
|
||||
String password, String newPassword) async {
|
||||
try {
|
||||
User? user = FirebaseAuth.instance.currentUser;
|
||||
|
||||
if (user == null) {
|
||||
return UpdatePassworErros.userNotFound;
|
||||
}
|
||||
|
||||
// Verificar la autenticación reciente
|
||||
await user.reauthenticateWithCredential(EmailAuthProvider.credential(
|
||||
email: user.email!,
|
||||
password: password,
|
||||
));
|
||||
|
||||
await _firebaseAuth.currentUser!.updatePassword(newPassword);
|
||||
return null;
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
return UpdatePassworErros.unknown;
|
||||
}
|
||||
}
|
||||
|
||||
// Sign out
|
||||
@override
|
||||
Future<void> logOut() async {
|
||||
try {
|
||||
await _firebaseAuth.signOut();
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> resetPassword(String email) async {
|
||||
try {
|
||||
await _firebaseAuth.sendPasswordResetEmail(email: email);
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setUserData(MyUser user) async {
|
||||
try {
|
||||
await usersCollection.doc(user.id).set(user.toEntity().toDocument());
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<MyUser?> getMyUser(String myUserId) async {
|
||||
try {
|
||||
return usersCollection.doc(myUserId).get().then((value) {
|
||||
final valueData = value.data();
|
||||
if (valueData == null || valueData.isEmpty || !value.exists) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return MyUser.fromEntity(
|
||||
MyUserEntity.fromDocument(valueData),
|
||||
);
|
||||
});
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateUserInfo(MyUser myUser) async {
|
||||
try {
|
||||
await usersCollection
|
||||
.doc(myUser.id)
|
||||
.update(myUser.toEntity().toDocument());
|
||||
await updateFromFirebase(myUser.id);
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> uploadPicture(String file, String userId) async {
|
||||
try {
|
||||
File imageFile = File(file);
|
||||
Reference firebaseStoreRef =
|
||||
FirebaseStorage.instance.ref().child('$userId/PP/${userId}_lead');
|
||||
await firebaseStoreRef.putFile(imageFile);
|
||||
String url = await firebaseStoreRef.getDownloadURL();
|
||||
return url;
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> createUser(MyUser myUser) async {
|
||||
try {
|
||||
await usersCollection.doc(myUser.id).set(myUser.toEntity().toDocument());
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<MyUser>> getUsersProfessionalActive() async {
|
||||
try {
|
||||
final querySnapshot = await usersCollection
|
||||
.where('professional_state', isEqualTo: ProState.active.index)
|
||||
.get();
|
||||
|
||||
return querySnapshot.docs
|
||||
.map((e) => MyUser.fromEntity(MyUserEntity.fromDocument(e.data())))
|
||||
.toList();
|
||||
} catch (e) {
|
||||
log('xd -- ${e.toString()}');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<MyUser>> getUsersFromIds(Iterable<String> ids) async {
|
||||
try {
|
||||
final querySnapshot =
|
||||
await usersCollection.where(FieldPath.documentId, whereIn: ids).get();
|
||||
|
||||
return querySnapshot.docs
|
||||
.map((e) => MyUser.fromEntity(MyUserEntity.fromDocument(e.data())))
|
||||
.toList();
|
||||
} catch (e) {
|
||||
log('getUsersFromIds ${e.toString()}');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user