signInByEmail method

Future<Response> signInByEmail(
  1. UserEntity entity
)

Implementation

Future<Response> signInByEmail(UserEntity entity) async {
  const cubitResponse = Response();
  final email = entity.email;
  final password = entity.password;
  if (!Validator.isValidEmail(email)) {
    emit(state.copy(error: "Email isn't valid!"));
    return cubitResponse.copy(message: "Email isn't valid!");
  } else if (!Validator.isValidPassword(password)) {
    emit(state.copy(error: "Password isn't valid!"));
    return cubitResponse.copy(message: "Password isn't valid!");
  } else {
    try {
      emit(state.copy(isLoading: true));
      final response = await handler.signInWithEmailNPassword(
        email: email,
        password: password,
      );
      final result = response.data?.user;
      if (result != null) {
        final user = entity.copy(
          id: result.uid,
          email: result.email,
          name: result.displayName,
          phone: result.phoneNumber,
          photo: result.photoURL,
          provider: AuthProvider.email.name,
        );
        final userResponse = await userHandler.create(user);
        if (userResponse.isSuccessful || userResponse.snapshot != null) {
          emit(state.copy(
            isLoggedIn: true,
            user: user,
            firebaseUser: result,
          ));
        } else {
          emit(state.copy(error: userResponse.message));
        }
        return userResponse.copy(data: user);
      } else {
        emit(state.copy(error: response.message));
        return response;
      }
    } catch (e) {
      emit(state.copy(error: e.toString()));
      return cubitResponse.copy(message: e.toString());
    }
  }
}