auth phone and re auth phone
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:user_repository/user_repository.dart';
|
||||
@@ -7,6 +10,9 @@ part 'auth_state.dart';
|
||||
|
||||
class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
final UserRepository _userRepository;
|
||||
final PhoneVerificationService phoneVerificationService =
|
||||
PhoneVerificationService();
|
||||
String? _verificationId;
|
||||
|
||||
AuthBloc({required UserRepository userRepository})
|
||||
: _userRepository = userRepository,
|
||||
@@ -14,6 +20,9 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
on<AuthEventLoginOAuth>(_onAuthEventLoginOAuth);
|
||||
on<AuthEventVerifyOAuth>(_onAuthEventVerifyOAuth);
|
||||
on<AuthEventAddEmailAndPassword>(_onAuthEventAddEmailAndPassword);
|
||||
on<AuthEventUpdatePassword>(_updatePassword);
|
||||
on<LinkWithPhoneNumber>(_linkWithPhoneNumber);
|
||||
on<LinkWithPhoneNumberOtp>(_linkWithPhoneNumberOtp);
|
||||
}
|
||||
|
||||
void _onAuthEventLoginOAuth(
|
||||
@@ -39,10 +48,6 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
} else {
|
||||
emit(const AuthStateVerifyOAuth(true));
|
||||
}
|
||||
|
||||
(isVerified)
|
||||
? emit(AuthStateSuccess())
|
||||
: emit(const AuthStateVerifyOAuth(true));
|
||||
} catch (e) {
|
||||
emit(const AuthStateFailure());
|
||||
}
|
||||
@@ -69,4 +74,161 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
emit(const AuthStateFailure());
|
||||
}
|
||||
}
|
||||
|
||||
void _updatePassword(
|
||||
AuthEventUpdatePassword event, Emitter<AuthState> emit) async {
|
||||
emit(AuthStateProcess());
|
||||
try {
|
||||
final error = await _userRepository.updatePassword(
|
||||
event.actualPassword, event.password);
|
||||
if (error == null) {
|
||||
emit(AuthStateSuccess());
|
||||
} else {
|
||||
switch (error) {
|
||||
case UpdatePassworErros.credentialsWrong:
|
||||
emit(const AuthStateFailure(message: "Contraseña incorrecta"));
|
||||
break;
|
||||
case UpdatePassworErros.userNotFound:
|
||||
emit(const AuthStateFailure(message: "Usuario no encontrado"));
|
||||
break;
|
||||
case UpdatePassworErros.unknown:
|
||||
emit(const AuthStateFailure(message: "Error inesperado. 🥸"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
emit(const AuthStateFailure(message: "Error inesperado. 🥸"));
|
||||
}
|
||||
}
|
||||
|
||||
void _linkWithPhoneNumber(
|
||||
LinkWithPhoneNumber event, Emitter<AuthState> emit) async {
|
||||
emit(AuthStateProcess());
|
||||
|
||||
try {
|
||||
await for (PhoneAuthEvent event
|
||||
in phoneVerificationService.verifyPhoneNumber(event.phoneNumber)) {
|
||||
switch (event.type) {
|
||||
case PhoneAuthEventType.verificationCompleted:
|
||||
AuthCredential credential = event.data;
|
||||
print('Verificación completada. Credencial: $credential');
|
||||
break;
|
||||
case PhoneAuthEventType.verificationFailed:
|
||||
FirebaseAuthException exception = event.data;
|
||||
print('Verificación fallida. Excepción: $exception');
|
||||
emit(AuthStateFailure(message: "Error inesperado. 🥸 $exception"));
|
||||
return; // Detener la ejecución aquí
|
||||
case PhoneAuthEventType.codeAutoRetrievalTimeout:
|
||||
String verificationId = event.data;
|
||||
print(
|
||||
'Tiempo de espera agotado para recuperar el código. ID: $verificationId');
|
||||
emit(const AuthStateFailure(
|
||||
message: "Tiempo de espera agotado. 🥸",
|
||||
));
|
||||
break;
|
||||
case PhoneAuthEventType.codeSent:
|
||||
Map<String, dynamic> eventData = event.data;
|
||||
String verificationId = eventData['verificationId'];
|
||||
int? resendToken = eventData['resendToken'];
|
||||
print(
|
||||
'Código enviado. ID: $verificationId, resendToken: $resendToken');
|
||||
_verificationId = verificationId;
|
||||
emit(const AuthStateVerifyOAuth(false));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
emit(const AuthStateFailure(message: "Error inesperado. PUTA 🥸"));
|
||||
}
|
||||
}
|
||||
|
||||
void _linkWithPhoneNumber3(
|
||||
LinkWithPhoneNumber event, Emitter<AuthState> emit) async {
|
||||
emit(AuthStateProcess());
|
||||
|
||||
try {
|
||||
StreamSubscription<PhoneAuthEvent> subscription = phoneVerificationService
|
||||
.verifyPhoneNumber(event.phoneNumber)
|
||||
.listen((event) async {
|
||||
switch (event.type) {
|
||||
case PhoneAuthEventType.verificationCompleted:
|
||||
AuthCredential credential = event.data;
|
||||
print('Verificación completada. Credencial: $credential');
|
||||
break;
|
||||
case PhoneAuthEventType.verificationFailed:
|
||||
FirebaseAuthException exception = event.data;
|
||||
print('Verificación fallida. Excepción: $exception');
|
||||
break;
|
||||
case PhoneAuthEventType.codeAutoRetrievalTimeout:
|
||||
String verificationId = event.data;
|
||||
print(
|
||||
'Tiempo de espera agotado para recuperar el código. ID: $verificationId');
|
||||
break;
|
||||
case PhoneAuthEventType.codeSent:
|
||||
Map<String, dynamic> eventData = event.data;
|
||||
String verificationId = eventData['verificationId'];
|
||||
int? resendToken = eventData['resendToken'];
|
||||
print(
|
||||
'Código enviado. ID: $verificationId, resendToken: $resendToken');
|
||||
_verificationId = verificationId;
|
||||
emit(const AuthStateVerifyOAuth(false));
|
||||
break;
|
||||
}
|
||||
|
||||
if (event.type == PhoneAuthEventType.verificationFailed) {
|
||||
// Detener la suscripción si la verificación falla
|
||||
// subscription.cancel();
|
||||
emit(const AuthStateFailure(message: "Error inesperado. 🥸"));
|
||||
}
|
||||
});
|
||||
|
||||
// await _userRepository
|
||||
} catch (e) {
|
||||
emit(const AuthStateFailure(message: "Error inesperado. PUTA 🥸"));
|
||||
}
|
||||
}
|
||||
|
||||
void _linkWithPhoneNumberOtp(
|
||||
LinkWithPhoneNumberOtp event, Emitter<AuthState> emit) async {
|
||||
emit(AuthStateProcess());
|
||||
try {
|
||||
final bool isVerified = await _userRepository.linkWithOTP(
|
||||
event.phoneNumber, _verificationId!, event.code);
|
||||
|
||||
if (isVerified) {
|
||||
emit(AuthStateSuccess());
|
||||
} else {
|
||||
emit(const AuthStateVerifyOAuth(true));
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is FirebaseAuthException) {
|
||||
switch (e.code) {
|
||||
case "invalid-verification-code":
|
||||
emit(const AuthStateFailure(
|
||||
message: "Código de verificación incorrecto. 🥸",
|
||||
));
|
||||
break;
|
||||
|
||||
case "provider-already-linked":
|
||||
emit(const AuthStateFailure(
|
||||
message: "Cuenta ya vinculada. 🥸",
|
||||
));
|
||||
break;
|
||||
|
||||
case "credential-already-in-use":
|
||||
emit(const AuthStateFailure(
|
||||
message:
|
||||
"Este numero ya se encuentra registrado con otra cuenta. 🥸",
|
||||
));
|
||||
break;
|
||||
|
||||
default:
|
||||
emit(AuthStateFailure(message: "Error inesperado. 🥸 $e"));
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
emit(AuthStateFailure(message: "Error inesperado. 🥸 $e"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,3 +31,39 @@ class AuthEventAddEmailAndPassword extends AuthEvent {
|
||||
@override
|
||||
List<Object> get props => [email, password];
|
||||
}
|
||||
|
||||
class AuthEventUpdatePassword extends AuthEvent {
|
||||
final String email;
|
||||
final String actualPassword;
|
||||
final String password;
|
||||
|
||||
const AuthEventUpdatePassword({
|
||||
required this.email,
|
||||
required this.actualPassword,
|
||||
required this.password,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object> get props => [password];
|
||||
}
|
||||
|
||||
class LinkWithPhoneNumber extends AuthEvent {
|
||||
final String phoneNumber;
|
||||
const LinkWithPhoneNumber({required this.phoneNumber});
|
||||
|
||||
@override
|
||||
List<Object> get props => [phoneNumber];
|
||||
}
|
||||
|
||||
class LinkWithPhoneNumberOtp extends AuthEvent {
|
||||
final String phoneNumber;
|
||||
final String code;
|
||||
|
||||
const LinkWithPhoneNumberOtp({
|
||||
required this.phoneNumber,
|
||||
required this.code,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object> get props => [phoneNumber, code];
|
||||
}
|
||||
|
||||
@@ -8,15 +8,13 @@ import 'package:user_repository/user_repository.dart';
|
||||
part 'authentication_event.dart';
|
||||
part 'authentication_state.dart';
|
||||
|
||||
class AuthenticationBloc
|
||||
extends Bloc<AuthenticationEvent, AuthenticationState> {
|
||||
class AuthenticationBloc extends Bloc<AuthenticationEvent, AuthenticationState> {
|
||||
final UserRepository userRepository;
|
||||
late final StreamSubscription<MyUser?> _userSubscription;
|
||||
|
||||
AuthenticationBloc({required UserRepository myUserRepository})
|
||||
: userRepository = myUserRepository,
|
||||
super(const AuthenticationState.unknown()) {
|
||||
_userSubscription = userRepository.streamUser().listen((authUser) {
|
||||
super(const AuthenticationState.unknown()) {_userSubscription = userRepository.streamUser().listen((authUser) {
|
||||
add(AuthenticationUserChanged(authUser));
|
||||
});
|
||||
on<AuthenticationUserChanged>(_onAuthenticationUserChanged);
|
||||
@@ -32,8 +30,7 @@ class AuthenticationBloc
|
||||
);
|
||||
}
|
||||
|
||||
void _onAuthenticationLogoutRequested(AuthenticationLogoutRequested event,
|
||||
Emitter<AuthenticationState> emit) async {
|
||||
void _onAuthenticationLogoutRequested(AuthenticationLogoutRequested event, Emitter<AuthenticationState> emit) async {
|
||||
await userRepository.logOut();
|
||||
emit(const AuthenticationState.unauthenticated());
|
||||
}
|
||||
|
||||
@@ -7,14 +7,6 @@ abstract class MyUserEvent extends Equatable {
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
// class GetMyUser extends MyUserEvent {
|
||||
// final String myUserId;
|
||||
|
||||
// const GetMyUser({required this.myUserId});
|
||||
|
||||
// @override
|
||||
// List<Object> get props => [myUserId];
|
||||
// }
|
||||
|
||||
class UserChanged extends MyUserEvent {
|
||||
final MyUser? user;
|
||||
|
||||
Reference in New Issue
Block a user