label
This commit is contained in:
@@ -2,14 +2,14 @@ import 'package:equatable/equatable.dart';
|
||||
|
||||
class MyUserEntity extends Equatable {
|
||||
final String id;
|
||||
final String email;
|
||||
final String name;
|
||||
final String? email;
|
||||
final String? name;
|
||||
final String? picture;
|
||||
|
||||
const MyUserEntity({
|
||||
required this.id,
|
||||
required this.email,
|
||||
required this.name,
|
||||
this.email,
|
||||
this.name,
|
||||
this.picture,
|
||||
});
|
||||
|
||||
@@ -25,8 +25,8 @@ class MyUserEntity extends Equatable {
|
||||
static MyUserEntity fromDocument(Map<String, dynamic> doc) {
|
||||
return MyUserEntity(
|
||||
id: doc['id'] as String,
|
||||
email: doc['email'] as String,
|
||||
name: doc['name'] as String,
|
||||
email: doc['email'] as String?,
|
||||
name: doc['name'] as String?,
|
||||
picture: doc['picture'] as String?,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,14 +4,14 @@ import '../entities/entities.dart';
|
||||
|
||||
class MyUser extends Equatable {
|
||||
final String id;
|
||||
final String email;
|
||||
final String name;
|
||||
String? picture;
|
||||
final String? email;
|
||||
final String? name;
|
||||
final String? picture;
|
||||
|
||||
MyUser({
|
||||
required this.id,
|
||||
required this.email,
|
||||
required this.name,
|
||||
this.email,
|
||||
this.name,
|
||||
this.picture,
|
||||
});
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ class FirebaseUserRepository implements UserRepository {
|
||||
final usersCollection = FirebaseFirestore.instance.collection('users');
|
||||
final StreamController<MyUser?> _userStreamController =
|
||||
StreamController<MyUser?>.broadcast();
|
||||
String verificationId = '';
|
||||
|
||||
FirebaseUserRepository(this._firebaseAuth) {
|
||||
_firebaseAuth.userChanges().listen((user) async {
|
||||
@@ -49,18 +50,14 @@ class FirebaseUserRepository implements UserRepository {
|
||||
|
||||
// Sign up
|
||||
@override
|
||||
Future<MyUser> signUp(MyUser myUser, String password) async {
|
||||
Future<MyUser> signUp(String email, String password) async {
|
||||
try {
|
||||
UserCredential user = await _firebaseAuth.createUserWithEmailAndPassword(
|
||||
email: myUser.email,
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
|
||||
myUser = myUser.copyWith(
|
||||
id: user.user!.uid,
|
||||
);
|
||||
|
||||
return myUser;
|
||||
return await getMyUser(user.user!.uid);
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
@@ -71,34 +68,21 @@ class FirebaseUserRepository implements UserRepository {
|
||||
@override
|
||||
Future<void> signInWithPhoneNumber(String phoneNumber) async {
|
||||
try {
|
||||
await FirebaseAuth.instance.verifyPhoneNumber(
|
||||
await _firebaseAuth.verifyPhoneNumber(
|
||||
phoneNumber: phoneNumber,
|
||||
verificationCompleted: (PhoneAuthCredential credential) async {
|
||||
// Esta función se llama automáticamente cuando se completa la verificación del número de teléfono.
|
||||
// Puedes usar 'credential' para iniciar sesión o vincular la cuenta.
|
||||
// En la mayoría de los casos, no necesitas implementar esto, ya que Firebase manejará la autenticación automáticamente.
|
||||
|
||||
await FirebaseAuth.instance.signInWithCredential(credential);
|
||||
},
|
||||
verificationFailed: (FirebaseAuthException e) {
|
||||
// Esta función se llama si la verificación del número de teléfono falla.
|
||||
// Maneja los errores o muestra un mensaje al usuario.
|
||||
if (e.code == 'invalid-phone-number') {
|
||||
// Manejar el caso de número de teléfono no válido
|
||||
} else if (e.code == 'network-request-failed') {
|
||||
// Manejar problemas de conectividad
|
||||
} else {
|
||||
// Manejar otros errores
|
||||
}
|
||||
await _firebaseAuth.signInWithCredential(credential);
|
||||
},
|
||||
codeSent: (String verificationId, int? resendToken) {
|
||||
// Esta función se llama cuando se envía el código de verificación al número de teléfono del usuario.
|
||||
// Debes guardar 'verificationId' para usarlo posteriormente en la verificación.
|
||||
// Puedes mostrar un diálogo para que el usuario ingrese el código o puedes verificarlo automáticamente.
|
||||
this.verificationId = verificationId;
|
||||
},
|
||||
codeAutoRetrievalTimeout: (String verificationId) {
|
||||
// Esta función se llama cuando el tiempo de espera de recuperación automática del código ha expirado.
|
||||
// Puedes manejar esto como prefieras, por ejemplo, mostrando un mensaje al usuario o reenviando el código.
|
||||
this.verificationId = verificationId;
|
||||
},
|
||||
verificationFailed: (FirebaseAuthException e) {
|
||||
if (e.code == 'invalid-phone-number') {
|
||||
} else if (e.code == 'network-request-failed') {
|
||||
} else {}
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
@@ -107,6 +91,26 @@ class FirebaseUserRepository implements UserRepository {
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> verifyOTP(String code) async {
|
||||
try {
|
||||
var credentials = await _firebaseAuth.signInWithCredential(
|
||||
PhoneAuthProvider.credential(
|
||||
verificationId: verificationId, smsCode: code));
|
||||
return credentials.user != null ? true : false;
|
||||
} catch (e) {
|
||||
if (e is FirebaseAuthException) {
|
||||
if (e.code == 'invalid-verification-code') {
|
||||
return false;
|
||||
} else {
|
||||
rethrow;
|
||||
}
|
||||
} else {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sign in
|
||||
@override
|
||||
Future<void> signIn(String email, String password) async {
|
||||
@@ -158,8 +162,12 @@ class FirebaseUserRepository implements UserRepository {
|
||||
@override
|
||||
Future<MyUser> getMyUser(String myUserId) async {
|
||||
try {
|
||||
return usersCollection.doc(myUserId).get().then((value) =>
|
||||
MyUser.fromEntity(MyUserEntity.fromDocument(value.data()!)));
|
||||
return usersCollection.doc(myUserId).get().then((value) {
|
||||
log('xd -- ${value.data().toString()}'); // Imprime el valor de value
|
||||
return MyUser.fromEntity(
|
||||
MyUserEntity.fromDocument(value.data()!),
|
||||
);
|
||||
});
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
|
||||
@@ -9,10 +9,12 @@ abstract class UserRepository {
|
||||
|
||||
Future<void> logOut();
|
||||
|
||||
Future<MyUser> signUp(MyUser myUser, String password);
|
||||
Future<MyUser> signUp(String email, String password);
|
||||
|
||||
Future<void> signInWithPhoneNumber(String phoneNumber);
|
||||
|
||||
Future<bool> verifyOTP(String code);
|
||||
|
||||
Future<void> resetPassword(String email);
|
||||
|
||||
Future<void> setUserData(MyUser user);
|
||||
|
||||
Reference in New Issue
Block a user