sign_in_bloc.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import 'package:appflowy/user/application/auth_service.dart';
  2. import 'package:dartz/dartz.dart';
  3. import 'package:appflowy_backend/protobuf/flowy-error/code.pb.dart';
  4. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  5. import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'
  6. show UserProfilePB;
  7. import 'package:freezed_annotation/freezed_annotation.dart';
  8. import 'package:flutter_bloc/flutter_bloc.dart';
  9. part 'sign_in_bloc.freezed.dart';
  10. class SignInBloc extends Bloc<SignInEvent, SignInState> {
  11. final AuthService authService;
  12. SignInBloc(this.authService) : super(SignInState.initial()) {
  13. on<SignInEvent>((event, emit) async {
  14. await event.map(
  15. signedInWithUserEmailAndPassword: (e) async {
  16. await _performActionOnSignIn(
  17. state,
  18. emit,
  19. );
  20. },
  21. emailChanged: (EmailChanged value) async {
  22. emit(
  23. state.copyWith(
  24. email: value.email,
  25. emailError: none(),
  26. successOrFail: none(),
  27. ),
  28. );
  29. },
  30. passwordChanged: (PasswordChanged value) async {
  31. emit(
  32. state.copyWith(
  33. password: value.password,
  34. passwordError: none(),
  35. successOrFail: none(),
  36. ),
  37. );
  38. },
  39. );
  40. });
  41. }
  42. Future<void> _performActionOnSignIn(
  43. SignInState state,
  44. Emitter<SignInState> emit,
  45. ) async {
  46. emit(
  47. state.copyWith(
  48. isSubmitting: true,
  49. emailError: none(),
  50. passwordError: none(),
  51. successOrFail: none(),
  52. ),
  53. );
  54. final result = await authService.signIn(
  55. email: state.email,
  56. password: state.password,
  57. );
  58. emit(
  59. result.fold(
  60. (userProfile) => state.copyWith(
  61. isSubmitting: false,
  62. successOrFail: some(left(userProfile)),
  63. ),
  64. (error) => stateFromCode(error),
  65. ),
  66. );
  67. }
  68. SignInState stateFromCode(FlowyError error) {
  69. switch (ErrorCode.valueOf(error.code)!) {
  70. case ErrorCode.EmailFormatInvalid:
  71. return state.copyWith(
  72. isSubmitting: false,
  73. emailError: some(error.msg),
  74. passwordError: none(),
  75. );
  76. case ErrorCode.PasswordFormatInvalid:
  77. return state.copyWith(
  78. isSubmitting: false,
  79. passwordError: some(error.msg),
  80. emailError: none(),
  81. );
  82. default:
  83. return state.copyWith(
  84. isSubmitting: false,
  85. successOrFail: some(right(error)),
  86. );
  87. }
  88. }
  89. }
  90. @freezed
  91. class SignInEvent with _$SignInEvent {
  92. const factory SignInEvent.signedInWithUserEmailAndPassword() =
  93. SignedInWithUserEmailAndPassword;
  94. const factory SignInEvent.emailChanged(String email) = EmailChanged;
  95. const factory SignInEvent.passwordChanged(String password) = PasswordChanged;
  96. }
  97. @freezed
  98. class SignInState with _$SignInState {
  99. const factory SignInState({
  100. String? email,
  101. String? password,
  102. required bool isSubmitting,
  103. required Option<String> passwordError,
  104. required Option<String> emailError,
  105. required Option<Either<UserProfilePB, FlowyError>> successOrFail,
  106. }) = _SignInState;
  107. factory SignInState.initial() => SignInState(
  108. isSubmitting: false,
  109. passwordError: none(),
  110. emailError: none(),
  111. successOrFail: none(),
  112. );
  113. }