AuthBloc constructor

AuthBloc()

Implementation

AuthBloc() : super(const AuthState()) {
  on<SendCode>((event, emit) async {
    emit(state.copyWith(isLoading: true));
    final res = await authRepository.sendCode(phone: event.phone);
    res.fold((l) async {
      emit(state.copyWith(isLoading: false));
      event.onSuccess?.call();
    }, (r) {
      AppHelper.errorSnackBar(
        context: event.context,
        message: r,
      );
    });
    emit(state.copyWith(isLoading: false));
  });

  on<CheckCode>((event, emit) async {
    emit(state.copyWith(isLoading: true));
    final res =
        await authRepository.checkCode(phone: event.phone, code: event.code);
    res.fold((l) async {
      LocalStorage.setToken(l["access-token"]);
      LocalStorage.setRefreshToken(l["refresh-token"]);
      emit(state.copyWith(isLoading: false));
      event.onSuccess?.call();
    }, (r) {
      AppHelper.errorSnackBar(
        context: event.context,
        message: r,
      );
    });
    emit(state.copyWith(isLoading: false));
  });

  on<Logout>((event, emit) async {
    emit(state.copyWith(isLoading: true));
    final res = await authRepository.logout();
    res.fold((l) async {
      emit(state.copyWith(isLoading: false));
      event.onSuccess?.call();
    }, (r) {
      AppHelper.errorSnackBar(
        context: event.context,
        message: r,
      );
    });
    emit(state.copyWith(isLoading: false));
  });
}