auth/wyatt_arch_migration #25
| @ -16,7 +16,8 @@ | |||||||
| 
 | 
 | ||||||
| import 'package:flutter/material.dart'; | import 'package:flutter/material.dart'; | ||||||
| import 'package:flutter_bloc/flutter_bloc.dart'; | import 'package:flutter_bloc/flutter_bloc.dart'; | ||||||
| import 'package:wyatt_authentication_bloc/src/domain/entities/user.dart'; | import 'package:wyatt_authentication_bloc/src/core/enums/authentication_status.dart'; | ||||||
|  | import 'package:wyatt_authentication_bloc/src/domain/entities/account_wrapper.dart'; | ||||||
| import 'package:wyatt_authentication_bloc/src/features/authentication/cubit/authentication_cubit.dart'; | import 'package:wyatt_authentication_bloc/src/features/authentication/cubit/authentication_cubit.dart'; | ||||||
| 
 | 
 | ||||||
| class AuthenticationBuilder<Extra> extends StatelessWidget { | class AuthenticationBuilder<Extra> extends StatelessWidget { | ||||||
| @ -29,8 +30,7 @@ class AuthenticationBuilder<Extra> extends StatelessWidget { | |||||||
| 
 | 
 | ||||||
|   final Widget Function( |   final Widget Function( | ||||||
|     BuildContext context, |     BuildContext context, | ||||||
|     User user, |     AccountWrapper<Extra> accountWrapper, | ||||||
|     Extra? extra, |  | ||||||
|   ) authenticated; |   ) authenticated; | ||||||
|   final Widget Function(BuildContext context) unauthenticated; |   final Widget Function(BuildContext context) unauthenticated; | ||||||
|   final Widget Function(BuildContext context) unknown; |   final Widget Function(BuildContext context) unknown; | ||||||
| @ -40,8 +40,8 @@ class AuthenticationBuilder<Extra> extends StatelessWidget { | |||||||
|       BlocBuilder<AuthenticationCubit<Extra>, AuthenticationState<Extra>>( |       BlocBuilder<AuthenticationCubit<Extra>, AuthenticationState<Extra>>( | ||||||
|         builder: (context, state) { |         builder: (context, state) { | ||||||
|           if (state.status == AuthenticationStatus.authenticated) { |           if (state.status == AuthenticationStatus.authenticated) { | ||||||
|             if (state.user != null) { |             if (state.accountWrapper != null) { | ||||||
|               return authenticated(context, state.user!, state.extra); |               return authenticated(context, state.accountWrapper!); | ||||||
|             } else { |             } else { | ||||||
|               return unauthenticated(context); |               return unauthenticated(context); | ||||||
|             } |             } | ||||||
|  | |||||||
| @ -18,83 +18,46 @@ import 'dart:async'; | |||||||
| 
 | 
 | ||||||
| import 'package:equatable/equatable.dart'; | import 'package:equatable/equatable.dart'; | ||||||
| import 'package:flutter_bloc/flutter_bloc.dart'; | import 'package:flutter_bloc/flutter_bloc.dart'; | ||||||
| import 'package:wyatt_authentication_bloc/src/core/enum/auth_cubit_status.dart'; | import 'package:wyatt_authentication_bloc/src/core/enums/authentication_status.dart'; | ||||||
| import 'package:wyatt_authentication_bloc/src/domain/entities/user.dart'; | import 'package:wyatt_authentication_bloc/src/domain/entities/account_wrapper.dart'; | ||||||
| import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart'; | import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart'; | ||||||
|  | import 'package:wyatt_type_utils/wyatt_type_utils.dart'; | ||||||
| 
 | 
 | ||||||
| part 'authentication_state.dart'; | part 'authentication_state.dart'; | ||||||
| 
 | 
 | ||||||
| class AuthenticationCubit<Extra> extends Cubit<AuthenticationState<Extra>> { | class AuthenticationCubit<Extra> extends Cubit<AuthenticationState<Extra>> { | ||||||
|   final AuthenticationRepository _authenticationRepository; |   final AuthenticationRepository<Extra> _authenticationRepository; | ||||||
|   late final StreamSubscription<AuthCubitStatus> _statusSubscription; |  | ||||||
| 
 |  | ||||||
|   StreamSubscription<User>? _userSubscription; |  | ||||||
| 
 |  | ||||||
|   final Future<Extra?> Function(User user)? _onAuthSuccess; |  | ||||||
| 
 | 
 | ||||||
|   AuthenticationCubit({ |   AuthenticationCubit({ | ||||||
|     required AuthenticationRepository authenticationRepository, |     required AuthenticationRepository<Extra> authenticationRepository, | ||||||
|     Future<Extra?> Function(User user)? onAuthSuccess, |  | ||||||
|   })  : _authenticationRepository = authenticationRepository, |   })  : _authenticationRepository = authenticationRepository, | ||||||
|         _onAuthSuccess = onAuthSuccess, |  | ||||||
|         super(const AuthenticationState.unknown()) { |         super(const AuthenticationState.unknown()) { | ||||||
|     _subscribeStatus(); |     _listenForAuthenticationChanges(); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   Future<AuthCubitStatus> get status async => |   void _listenForAuthenticationChanges() { | ||||||
|       _authenticationRepository.cubitStatus.last; |     _authenticationRepository.streamAccount().listen((accountFutureResult) { | ||||||
| 
 |       accountFutureResult.fold( | ||||||
|   void _subscribeStatus() { |         (value) { | ||||||
|     try { |           if (value.account.isNotNull) { | ||||||
|       _statusSubscription = _authenticationRepository.cubitStatus.listen( |             emit(AuthenticationState.authenticated(value)); | ||||||
|         (status) { |             return; | ||||||
|           switch (status) { |  | ||||||
|             case AuthCubitStatus.started: |  | ||||||
|               start(); |  | ||||||
|               break; |  | ||||||
|             case AuthCubitStatus.stoped: |  | ||||||
|               stop(); |  | ||||||
|               break; |  | ||||||
|           } |           } | ||||||
|  |           _authenticationRepository.destroyCache(); | ||||||
|  |           emit(const AuthenticationState.unauthenticated()); | ||||||
|  |           return; | ||||||
|  |         }, | ||||||
|  |         (error) { | ||||||
|  |           _authenticationRepository.destroyCache(); | ||||||
|  |           emit(const AuthenticationState.unauthenticated()); | ||||||
|  |           return; | ||||||
|         }, |         }, | ||||||
|       ); |       ); | ||||||
|     } catch (_) {} |     }); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   Future<void> init() async { |   FutureOr<void> signOut() { | ||||||
|     final firstUser = await _authenticationRepository.user.first; |     // TODO(hpcl): maybe force unauthenticated by emitting an event | ||||||
|     _authenticationRepository.changeCubitStatus(AuthCubitStatus.started); |     _authenticationRepository.signOut(); | ||||||
|     return changeStatus(firstUser); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   bool start() { |  | ||||||
|     _userSubscription = _authenticationRepository.user.listen(changeStatus); |  | ||||||
|     return true; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   bool stop() { |  | ||||||
|     _userSubscription?.cancel(); |  | ||||||
|     return true; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   Future<void> changeStatus(User user) async { |  | ||||||
|     if (user.isNotEmpty) { |  | ||||||
|       final Extra? extra = await _onAuthSuccess?.call(user); |  | ||||||
|       emit(AuthenticationState.authenticated(user, extra)); |  | ||||||
|     } else { |  | ||||||
|       _authenticationRepository.changeCubitStatus(AuthCubitStatus.stoped); |  | ||||||
|       emit(const AuthenticationState.unauthenticated()); |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   void logOut() { |  | ||||||
|     unawaited(_authenticationRepository.signOut()); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   @override |  | ||||||
|   Future<void> close() { |  | ||||||
|     _userSubscription?.cancel(); |  | ||||||
|     _statusSubscription.cancel(); |  | ||||||
|     return super.close(); |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -16,42 +16,28 @@ | |||||||
| 
 | 
 | ||||||
| part of 'authentication_cubit.dart'; | part of 'authentication_cubit.dart'; | ||||||
| 
 | 
 | ||||||
| enum AuthenticationStatus { |  | ||||||
|   unknown, |  | ||||||
|   authenticated, |  | ||||||
|   unauthenticated, |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| class AuthenticationState<Extra> extends Equatable { | class AuthenticationState<Extra> extends Equatable { | ||||||
|   final AuthenticationStatus status; |   final AuthenticationStatus status; | ||||||
|   final User? user; |   final AccountWrapper<Extra>? accountWrapper; | ||||||
|   final Extra? extra; |  | ||||||
| 
 | 
 | ||||||
|   const AuthenticationState._({ |   const AuthenticationState._({required this.status, this.accountWrapper}); | ||||||
|     required this.status, |  | ||||||
|     this.user, |  | ||||||
|     this.extra, |  | ||||||
|   }); |  | ||||||
| 
 | 
 | ||||||
|   const AuthenticationState.unknown() |   const AuthenticationState.unknown() | ||||||
|       : this._(status: AuthenticationStatus.unknown); |       : this._(status: AuthenticationStatus.unknown); | ||||||
| 
 | 
 | ||||||
|   const AuthenticationState.authenticated( |   const AuthenticationState.authenticated(AccountWrapper<Extra> accountWrapper) | ||||||
|     User user, |       : this._( | ||||||
|     Extra? extra, |  | ||||||
|   ) : this._( |  | ||||||
|           status: AuthenticationStatus.authenticated, |           status: AuthenticationStatus.authenticated, | ||||||
|           user: user, |           accountWrapper: accountWrapper, | ||||||
|           extra: extra, |  | ||||||
|         ); |         ); | ||||||
| 
 | 
 | ||||||
|   const AuthenticationState.unauthenticated() |   const AuthenticationState.unauthenticated() | ||||||
|       : this._(status: AuthenticationStatus.unauthenticated); |       : this._(status: AuthenticationStatus.unauthenticated); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   List<Object?> get props => [status, user, extra]; |   List<Object?> get props => [status]; | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   String toString() => |   String toString() => | ||||||
|       'AuthenticationState(status: $status, user: $user, extra: $extra)'; |       'AuthenticationState(status: $status, accountWrapper: $accountWrapper)'; | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,71 +0,0 @@ | |||||||
| // Copyright (C) 2022 WYATT GROUP |  | ||||||
| // Please see the AUTHORS file for details. |  | ||||||
| // |  | ||||||
| // This program is free software: you can redistribute it and/or modify |  | ||||||
| // it under the terms of the GNU General Public License as published by |  | ||||||
| // the Free Software Foundation, either version 3 of the License, or |  | ||||||
| // any later version. |  | ||||||
| // |  | ||||||
| // This program is distributed in the hope that it will be useful, |  | ||||||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of |  | ||||||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |  | ||||||
| // GNU General Public License for more details. |  | ||||||
| // |  | ||||||
| // You should have received a copy of the GNU General Public License |  | ||||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. |  | ||||||
| 
 |  | ||||||
| import 'package:equatable/equatable.dart'; |  | ||||||
| import 'package:flutter_bloc/flutter_bloc.dart'; |  | ||||||
| import 'package:wyatt_authentication_bloc/src/core/exceptions/exceptions.dart'; |  | ||||||
| import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart'; |  | ||||||
| import 'package:wyatt_form_bloc/wyatt_form_bloc.dart' show FormStatus; |  | ||||||
| 
 |  | ||||||
| part 'email_verification_state.dart'; |  | ||||||
| 
 |  | ||||||
| class EmailVerificationCubit extends Cubit<EmailVerificationState> { |  | ||||||
|   final AuthenticationRepository _authenticationRepository; |  | ||||||
| 
 |  | ||||||
|   EmailVerificationCubit({ |  | ||||||
|     required AuthenticationRepository authenticationRepository, |  | ||||||
|   })  : _authenticationRepository = authenticationRepository, |  | ||||||
|         super(const EmailVerificationState()); |  | ||||||
| 
 |  | ||||||
|   Future<void> sendEmailVerification() async { |  | ||||||
|     emit(state.copyWith(status: FormStatus.submissionInProgress)); |  | ||||||
|     try { |  | ||||||
|       await _authenticationRepository.sendEmailVerification(); |  | ||||||
|       emit(state.copyWith(status: FormStatus.submissionSuccess)); |  | ||||||
|     } on SendEmailVerificationFailureInterface catch (e) { |  | ||||||
|       emit( |  | ||||||
|         state.copyWith( |  | ||||||
|           errorMessage: e.message, |  | ||||||
|           status: FormStatus.submissionFailure, |  | ||||||
|         ), |  | ||||||
|       ); |  | ||||||
|     } catch (_) { |  | ||||||
|       emit(state.copyWith(status: FormStatus.submissionFailure)); |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   Future<void> checkEmailVerification() async { |  | ||||||
|     emit(state.copyWith(status: FormStatus.submissionInProgress)); |  | ||||||
|     try { |  | ||||||
|       await _authenticationRepository.refresh(); |  | ||||||
|       emit( |  | ||||||
|         state.copyWith( |  | ||||||
|           isVerified: _authenticationRepository.currentUser.emailVerified, |  | ||||||
|           status: FormStatus.submissionSuccess, |  | ||||||
|         ), |  | ||||||
|       ); |  | ||||||
|     } on RefreshFailureInterface catch (e) { |  | ||||||
|       emit( |  | ||||||
|         state.copyWith( |  | ||||||
|           errorMessage: e.message, |  | ||||||
|           status: FormStatus.submissionFailure, |  | ||||||
|         ), |  | ||||||
|       ); |  | ||||||
|     } catch (_) { |  | ||||||
|       emit(state.copyWith(status: FormStatus.submissionFailure)); |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -1,42 +0,0 @@ | |||||||
| // Copyright (C) 2022 WYATT GROUP |  | ||||||
| // Please see the AUTHORS file for details. |  | ||||||
| // |  | ||||||
| // This program is free software: you can redistribute it and/or modify |  | ||||||
| // it under the terms of the GNU General Public License as published by |  | ||||||
| // the Free Software Foundation, either version 3 of the License, or |  | ||||||
| // any later version. |  | ||||||
| // |  | ||||||
| // This program is distributed in the hope that it will be useful, |  | ||||||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of |  | ||||||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |  | ||||||
| // GNU General Public License for more details. |  | ||||||
| // |  | ||||||
| // You should have received a copy of the GNU General Public License |  | ||||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. |  | ||||||
| 
 |  | ||||||
| part of 'email_verification_cubit.dart'; |  | ||||||
| 
 |  | ||||||
| class EmailVerificationState extends Equatable { |  | ||||||
|   final bool isVerified; |  | ||||||
|   final FormStatus status; |  | ||||||
|   final String? errorMessage; |  | ||||||
| 
 |  | ||||||
|   const EmailVerificationState({ |  | ||||||
|     this.isVerified = false, |  | ||||||
|     this.status = FormStatus.pure, |  | ||||||
|     this.errorMessage, |  | ||||||
|   }); |  | ||||||
| 
 |  | ||||||
|   EmailVerificationState copyWith({ |  | ||||||
|     bool? isVerified, |  | ||||||
|     FormStatus? status, |  | ||||||
|     String? errorMessage, |  | ||||||
|   }) => EmailVerificationState( |  | ||||||
|       isVerified: isVerified ?? this.isVerified, |  | ||||||
|       status: status ?? this.status, |  | ||||||
|       errorMessage: errorMessage ?? this.errorMessage, |  | ||||||
|     ); |  | ||||||
| 
 |  | ||||||
|   @override |  | ||||||
|   List<Object> get props => [isVerified, status]; |  | ||||||
| } |  | ||||||
| @ -14,4 +14,6 @@ | |||||||
| // You should have received a copy of the GNU General Public License | // You should have received a copy of the GNU General Public License | ||||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||
| 
 | 
 | ||||||
| export 'cubit/email_verification_cubit.dart'; | export 'authentication/authentication.dart'; | ||||||
|  | export 'sign_in/sign_in.dart'; | ||||||
|  | export 'sign_up/sign_up.dart'; | ||||||
| @ -1,68 +0,0 @@ | |||||||
| // Copyright (C) 2022 WYATT GROUP |  | ||||||
| // Please see the AUTHORS file for details. |  | ||||||
| // |  | ||||||
| // This program is free software: you can redistribute it and/or modify |  | ||||||
| // it under the terms of the GNU General Public License as published by |  | ||||||
| // the Free Software Foundation, either version 3 of the License, or |  | ||||||
| // any later version. |  | ||||||
| // |  | ||||||
| // This program is distributed in the hope that it will be useful, |  | ||||||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of |  | ||||||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |  | ||||||
| // GNU General Public License for more details. |  | ||||||
| // |  | ||||||
| // You should have received a copy of the GNU General Public License |  | ||||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. |  | ||||||
| 
 |  | ||||||
| import 'package:equatable/equatable.dart'; |  | ||||||
| import 'package:flutter_bloc/flutter_bloc.dart'; |  | ||||||
| import 'package:wyatt_authentication_bloc/src/core/exceptions/exceptions.dart'; |  | ||||||
| import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart'; |  | ||||||
| import 'package:wyatt_form_bloc/wyatt_form_bloc.dart'; |  | ||||||
| 
 |  | ||||||
| part 'password_reset_state.dart'; |  | ||||||
| 
 |  | ||||||
| class PasswordResetCubit extends Cubit<PasswordResetState> { |  | ||||||
|   final AuthenticationRepository _authenticationRepository; |  | ||||||
| 
 |  | ||||||
|   final FormValidator _validationStrategy; |  | ||||||
| 
 |  | ||||||
|   PasswordResetCubit({ |  | ||||||
|     required AuthenticationRepository authenticationRepository, |  | ||||||
|     FormValidator validationStrategy = const EveryInputValidator(), |  | ||||||
|   })  : _authenticationRepository = authenticationRepository, |  | ||||||
|         _validationStrategy = validationStrategy, |  | ||||||
|         super(const PasswordResetState()); |  | ||||||
| 
 |  | ||||||
|   void emailChanged(String value) { |  | ||||||
|     final Email email = Email.dirty(value); |  | ||||||
|     emit( |  | ||||||
|       state.copyWith( |  | ||||||
|         email: email, |  | ||||||
|         status: _validationStrategy.rawValidate([email]), |  | ||||||
|       ), |  | ||||||
|     ); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   Future<void> sendPasswordResetEmail() async { |  | ||||||
|     if (!state.status.isValidated) { |  | ||||||
|       return; |  | ||||||
|     } |  | ||||||
|     emit(state.copyWith(status: FormStatus.submissionInProgress)); |  | ||||||
|     try { |  | ||||||
|       await _authenticationRepository.sendPasswordResetEmail( |  | ||||||
|         email: state.email.value, |  | ||||||
|       ); |  | ||||||
|       emit(state.copyWith(status: FormStatus.submissionSuccess)); |  | ||||||
|     } on SendPasswordResetEmailFailureInterface catch (e) { |  | ||||||
|       emit( |  | ||||||
|         state.copyWith( |  | ||||||
|           errorMessage: e.message, |  | ||||||
|           status: FormStatus.submissionFailure, |  | ||||||
|         ), |  | ||||||
|       ); |  | ||||||
|     } catch (_) { |  | ||||||
|       emit(state.copyWith(status: FormStatus.submissionFailure)); |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -1,42 +0,0 @@ | |||||||
| // Copyright (C) 2022 WYATT GROUP |  | ||||||
| // Please see the AUTHORS file for details. |  | ||||||
| //  |  | ||||||
| // This program is free software: you can redistribute it and/or modify |  | ||||||
| // it under the terms of the GNU General Public License as published by |  | ||||||
| // the Free Software Foundation, either version 3 of the License, or |  | ||||||
| // any later version. |  | ||||||
| //  |  | ||||||
| // This program is distributed in the hope that it will be useful, |  | ||||||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of |  | ||||||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |  | ||||||
| // GNU General Public License for more details. |  | ||||||
| //  |  | ||||||
| // You should have received a copy of the GNU General Public License |  | ||||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. |  | ||||||
| 
 |  | ||||||
| part of 'password_reset_cubit.dart'; |  | ||||||
| 
 |  | ||||||
| class PasswordResetState extends Equatable { |  | ||||||
|   final Email email; |  | ||||||
|   final FormStatus status; |  | ||||||
|   final String? errorMessage; |  | ||||||
| 
 |  | ||||||
|   const PasswordResetState({ |  | ||||||
|     this.email = const Email.pure(), |  | ||||||
|     this.status = FormStatus.pure, |  | ||||||
|     this.errorMessage, |  | ||||||
|   }); |  | ||||||
| 
 |  | ||||||
|   PasswordResetState copyWith({ |  | ||||||
|     Email? email, |  | ||||||
|     FormStatus? status, |  | ||||||
|     String? errorMessage, |  | ||||||
|   }) => PasswordResetState( |  | ||||||
|       email: email ?? this.email, |  | ||||||
|       status: status ?? this.status, |  | ||||||
|       errorMessage: errorMessage ?? this.errorMessage, |  | ||||||
|     ); |  | ||||||
| 
 |  | ||||||
|   @override |  | ||||||
|   List<Object> get props => [email, status]; |  | ||||||
| } |  | ||||||
| @ -1,17 +0,0 @@ | |||||||
| // Copyright (C) 2022 WYATT GROUP |  | ||||||
| // Please see the AUTHORS file for details. |  | ||||||
| // |  | ||||||
| // This program is free software: you can redistribute it and/or modify |  | ||||||
| // it under the terms of the GNU General Public License as published by |  | ||||||
| // the Free Software Foundation, either version 3 of the License, or |  | ||||||
| // any later version. |  | ||||||
| // |  | ||||||
| // This program is distributed in the hope that it will be useful, |  | ||||||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of |  | ||||||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |  | ||||||
| // GNU General Public License for more details. |  | ||||||
| // |  | ||||||
| // You should have received a copy of the GNU General Public License |  | ||||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. |  | ||||||
| 
 |  | ||||||
| export 'cubit/password_reset_cubit.dart'; |  | ||||||
| @ -16,20 +16,18 @@ | |||||||
| 
 | 
 | ||||||
| import 'package:equatable/equatable.dart'; | import 'package:equatable/equatable.dart'; | ||||||
| import 'package:flutter_bloc/flutter_bloc.dart'; | import 'package:flutter_bloc/flutter_bloc.dart'; | ||||||
| import 'package:wyatt_authentication_bloc/src/core/enum/auth_cubit_status.dart'; |  | ||||||
| import 'package:wyatt_authentication_bloc/src/core/exceptions/exceptions.dart'; |  | ||||||
| import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart'; | import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart'; | ||||||
| import 'package:wyatt_form_bloc/wyatt_form_bloc.dart'; | import 'package:wyatt_form_bloc/wyatt_form_bloc.dart'; | ||||||
| 
 | 
 | ||||||
| part 'sign_in_state.dart'; | part 'sign_in_state.dart'; | ||||||
| 
 | 
 | ||||||
| class SignInCubit extends Cubit<SignInState> { | class SignInCubit<Extra> extends Cubit<SignInState> { | ||||||
|   final AuthenticationRepository _authenticationRepository; |   final AuthenticationRepository<Extra> _authenticationRepository; | ||||||
| 
 | 
 | ||||||
|   final FormValidator _validationStrategy; |   final FormValidator _validationStrategy; | ||||||
| 
 | 
 | ||||||
|   SignInCubit({ |   SignInCubit({ | ||||||
|     required AuthenticationRepository authenticationRepository, |     required AuthenticationRepository<Extra> authenticationRepository, | ||||||
|     FormValidator validationStrategy = const EveryInputValidator(), |     FormValidator validationStrategy = const EveryInputValidator(), | ||||||
|   })  : _authenticationRepository = authenticationRepository, |   })  : _authenticationRepository = authenticationRepository, | ||||||
|         _validationStrategy = validationStrategy, |         _validationStrategy = validationStrategy, | ||||||
| @ -55,71 +53,26 @@ class SignInCubit extends Cubit<SignInState> { | |||||||
|     ); |     ); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   Future<void> signInAnonymously() async { |  | ||||||
|     if (state.status.isSubmissionInProgress) { |  | ||||||
|       return; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     emit(state.copyWith(status: FormStatus.submissionInProgress)); |  | ||||||
|     try { |  | ||||||
|       await _authenticationRepository.signInAnonymously(); |  | ||||||
|       _authenticationRepository.changeCubitStatus(AuthCubitStatus.started); |  | ||||||
|       emit(state.copyWith(status: FormStatus.submissionSuccess)); |  | ||||||
|     } on SignInAnonymouslyFailureInterface catch (e) { |  | ||||||
|       emit( |  | ||||||
|         state.copyWith( |  | ||||||
|           errorMessage: e.message, |  | ||||||
|           status: FormStatus.submissionFailure, |  | ||||||
|         ), |  | ||||||
|       ); |  | ||||||
|     } catch (_) { |  | ||||||
|       emit(state.copyWith(status: FormStatus.submissionFailure)); |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   Future<void> signInWithGoogle() async { |  | ||||||
|     if (state.status.isSubmissionInProgress) { |  | ||||||
|       return; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     emit(state.copyWith(status: FormStatus.submissionInProgress)); |  | ||||||
|     try { |  | ||||||
|       await _authenticationRepository.signInWithGoogle(); |  | ||||||
|       _authenticationRepository.changeCubitStatus(AuthCubitStatus.started); |  | ||||||
|       emit(state.copyWith(status: FormStatus.submissionSuccess)); |  | ||||||
|     } on SignInWithGoogleFailureInterface catch (e) { |  | ||||||
|       emit( |  | ||||||
|         state.copyWith( |  | ||||||
|           errorMessage: e.message, |  | ||||||
|           status: FormStatus.submissionFailure, |  | ||||||
|         ), |  | ||||||
|       ); |  | ||||||
|     } catch (_) { |  | ||||||
|       emit(state.copyWith(status: FormStatus.submissionFailure)); |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   Future<void> signInWithEmailAndPassword() async { |   Future<void> signInWithEmailAndPassword() async { | ||||||
|     if (!state.status.isValidated) { |     if (!state.status.isValidated) { | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|     emit(state.copyWith(status: FormStatus.submissionInProgress)); |     emit(state.copyWith(status: FormStatus.submissionInProgress)); | ||||||
|     try { | 
 | ||||||
|       await _authenticationRepository.signInWithEmailAndPassword( |     final uid = await _authenticationRepository.signInWithEmailAndPassword( | ||||||
|       email: state.email.value, |       email: state.email.value, | ||||||
|       password: state.password.value, |       password: state.password.value, | ||||||
|     ); |     ); | ||||||
|       _authenticationRepository.changeCubitStatus(AuthCubitStatus.started); | 
 | ||||||
|       emit(state.copyWith(status: FormStatus.submissionSuccess)); |  | ||||||
|     } on SignInWithEmailAndPasswordFailureInterface catch (e) { |  | ||||||
|     emit( |     emit( | ||||||
|         state.copyWith( |       uid.fold( | ||||||
|           errorMessage: e.message, |         (value) => state.copyWith(status: FormStatus.submissionSuccess), | ||||||
|  |         (error) => state.copyWith( | ||||||
|  |           errorMessage: error.message, | ||||||
|           status: FormStatus.submissionFailure, |           status: FormStatus.submissionFailure, | ||||||
|         ), |         ), | ||||||
|  |       ), | ||||||
|     ); |     ); | ||||||
|     } catch (_) { |  | ||||||
|       emit(state.copyWith(status: FormStatus.submissionFailure)); |  | ||||||
|     } |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -17,28 +17,22 @@ | |||||||
| import 'dart:async'; | import 'dart:async'; | ||||||
| 
 | 
 | ||||||
| import 'package:equatable/equatable.dart'; | import 'package:equatable/equatable.dart'; | ||||||
| import 'package:flutter/foundation.dart'; |  | ||||||
| import 'package:flutter_bloc/flutter_bloc.dart'; | import 'package:flutter_bloc/flutter_bloc.dart'; | ||||||
| import 'package:wyatt_authentication_bloc/src/core/enum/auth_cubit_status.dart'; |  | ||||||
| import 'package:wyatt_authentication_bloc/src/core/exceptions/exceptions.dart'; |  | ||||||
| import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart'; | import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart'; | ||||||
| import 'package:wyatt_form_bloc/wyatt_form_bloc.dart'; | import 'package:wyatt_form_bloc/wyatt_form_bloc.dart'; | ||||||
| 
 | 
 | ||||||
| part 'sign_up_state.dart'; | part 'sign_up_state.dart'; | ||||||
| 
 | 
 | ||||||
| class SignUpCubit extends Cubit<SignUpState> { | class SignUpCubit<Extra> extends Cubit<SignUpState> { | ||||||
|   final AuthenticationRepository _authenticationRepository; |   final AuthenticationRepository<Extra> _authenticationRepository; | ||||||
| 
 | 
 | ||||||
|   final Future<void> Function(SignUpState state, String? uid)? _onSignUpSuccess; |  | ||||||
|   final FormValidator _validationStrategy; |   final FormValidator _validationStrategy; | ||||||
| 
 | 
 | ||||||
|   SignUpCubit({ |   SignUpCubit({ | ||||||
|     required AuthenticationRepository authenticationRepository, |     required AuthenticationRepository<Extra> authenticationRepository, | ||||||
|     required FormData formData, |     required FormData formData, | ||||||
|     FormValidator validationStrategy = const EveryInputValidator(), |     FormValidator validationStrategy = const EveryInputValidator(), | ||||||
|     Future<void> Function(SignUpState state, String? uid)? onSignUpSuccess, |  | ||||||
|   })  : _authenticationRepository = authenticationRepository, |   })  : _authenticationRepository = authenticationRepository, | ||||||
|         _onSignUpSuccess = onSignUpSuccess, |  | ||||||
|         _validationStrategy = validationStrategy, |         _validationStrategy = validationStrategy, | ||||||
|         super(SignUpState(data: formData)); |         super(SignUpState(data: formData)); | ||||||
| 
 | 
 | ||||||
| @ -144,24 +138,20 @@ class SignUpCubit extends Cubit<SignUpState> { | |||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     emit(state.copyWith(status: FormStatus.submissionInProgress)); |     emit(state.copyWith(status: FormStatus.submissionInProgress)); | ||||||
|     try { | 
 | ||||||
|     final uid = await _authenticationRepository.signUp( |     final uid = await _authenticationRepository.signUp( | ||||||
|       email: state.email.value, |       email: state.email.value, | ||||||
|       password: state.password.value, |       password: state.password.value, | ||||||
|     ); |     ); | ||||||
|       await _onSignUpSuccess?.call(state, uid); | 
 | ||||||
|       _authenticationRepository.changeCubitStatus(AuthCubitStatus.started); |  | ||||||
|       emit(state.copyWith(status: FormStatus.submissionSuccess)); |  | ||||||
|     } on SignUpWithEmailAndPasswordFailureInterface catch (e) { |  | ||||||
|     emit( |     emit( | ||||||
|         state.copyWith( |       uid.fold( | ||||||
|           errorMessage: e.message, |         (value) => state.copyWith(status: FormStatus.submissionSuccess), | ||||||
|  |         (error) => state.copyWith( | ||||||
|  |           errorMessage: error.message, | ||||||
|           status: FormStatus.submissionFailure, |           status: FormStatus.submissionFailure, | ||||||
|         ), |         ), | ||||||
|  |       ), | ||||||
|     ); |     ); | ||||||
|     } catch (e) { |  | ||||||
|       debugPrint(e.toString()); |  | ||||||
|       emit(state.copyWith(status: FormStatus.submissionFailure)); |  | ||||||
|     } |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -14,17 +14,7 @@ | |||||||
| // You should have received a copy of the GNU General Public License | // You should have received a copy of the GNU General Public License | ||||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||
| 
 | 
 | ||||||
| export 'core/enum/auth_cubit_status.dart'; | export 'core/core.dart'; | ||||||
| export 'core/exceptions/exceptions.dart'; | export 'data/data.dart'; | ||||||
| export 'core/exceptions/exceptions_firebase.dart'; | export 'domain/domain.dart'; | ||||||
| export 'core/extensions/firebase_auth_user_x.dart'; | export 'features/features.dart'; | ||||||
| export 'core/utils/cryptography.dart'; |  | ||||||
| export 'data/models/user_firebase.dart'; |  | ||||||
| export 'data/repositories/authentication_repository_firebase.dart'; |  | ||||||
| export 'domain/entities/user.dart'; |  | ||||||
| export 'domain/repositories/authentication_repository.dart'; |  | ||||||
| export 'features/authentication/authentication.dart'; |  | ||||||
| export 'features/email_verification/email_verification.dart'; |  | ||||||
| export 'features/password_reset/password_reset.dart'; |  | ||||||
| export 'features/sign_in/sign_in.dart'; |  | ||||||
| export 'features/sign_up/sign_up.dart'; |  | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user