61 lines
1.7 KiB
Dart
61 lines
1.7 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:user_repository/user_repository.dart';
|
|
|
|
part 'auth_event.dart';
|
|
part 'auth_state.dart';
|
|
|
|
class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
|
final UserRepository _userRepository;
|
|
|
|
AuthBloc({required UserRepository userRepository})
|
|
: _userRepository = userRepository,
|
|
super(AuthStateInitial()) {
|
|
on<AuthEventLoginOAuth>(_onAuthEventLoginOAuth);
|
|
on<AuthEventVerifyOAuth>(_onAuthEventVerifyOAuth);
|
|
on<AuthEventAddEmailAndPassword>(_onAuthEventAddEmailAndPassword);
|
|
}
|
|
|
|
void _onAuthEventLoginOAuth(
|
|
AuthEventLoginOAuth event, Emitter<AuthState> emit) async {
|
|
emit(AuthStateProcess());
|
|
try {
|
|
await _userRepository.signInWithPhoneNumber(event.phone);
|
|
|
|
emit(const AuthStateVerifyOAuth(false));
|
|
} catch (e) {
|
|
emit(const AuthStateFailure());
|
|
}
|
|
}
|
|
|
|
void _onAuthEventVerifyOAuth(
|
|
AuthEventVerifyOAuth event, Emitter<AuthState> emit) async {
|
|
emit(AuthStateProcess());
|
|
try {
|
|
final bool isVerified = await _userRepository.verifyOTP(event.code);
|
|
|
|
if (isVerified) {
|
|
emit(AuthStateSuccess());
|
|
} else {
|
|
emit(const AuthStateVerifyOAuth(true));
|
|
}
|
|
|
|
(isVerified)
|
|
? emit(AuthStateSuccess())
|
|
: emit(const AuthStateVerifyOAuth(true));
|
|
} catch (e) {
|
|
emit(const AuthStateFailure());
|
|
}
|
|
}
|
|
|
|
void _onAuthEventAddEmailAndPassword(
|
|
AuthEventAddEmailAndPassword event, Emitter<AuthState> emit) async {
|
|
emit(AuthStateProcess());
|
|
try {
|
|
await _userRepository.addEmailAndPassword(event.email, event.password);
|
|
} catch (e) {
|
|
emit(const AuthStateFailure());
|
|
}
|
|
}
|
|
}
|