From 33c758e71190c25ec12857b1a2712cce0cfdaaf0 Mon Sep 17 00:00:00 2001 From: MikeWallaceDev Date: Tue, 1 Mar 2022 10:22:28 -0500 Subject: [PATCH] refactor: put Auth business logic in a service --- .../auth_repo.dart => application/auth_service.dart} | 2 +- frontend/app_flowy/lib/user/application/sign_in_bloc.dart | 8 ++++---- frontend/app_flowy/lib/user/application/sign_up_bloc.dart | 8 ++++---- .../app_flowy/lib/user/infrastructure/deps_resolver.dart | 8 ++++---- frontend/app_flowy/lib/user/infrastructure/router.dart | 4 ++-- .../lib/user/presentation/skip_log_in_screen.dart | 8 ++++---- 6 files changed, 19 insertions(+), 19 deletions(-) rename frontend/app_flowy/lib/user/{infrastructure/repos/auth_repo.dart => application/auth_service.dart} (98%) diff --git a/frontend/app_flowy/lib/user/infrastructure/repos/auth_repo.dart b/frontend/app_flowy/lib/user/application/auth_service.dart similarity index 98% rename from frontend/app_flowy/lib/user/infrastructure/repos/auth_repo.dart rename to frontend/app_flowy/lib/user/application/auth_service.dart index 69d4edebe3..1bbc0e8d19 100644 --- a/frontend/app_flowy/lib/user/infrastructure/repos/auth_repo.dart +++ b/frontend/app_flowy/lib/user/application/auth_service.dart @@ -3,7 +3,7 @@ import 'package:flowy_sdk/dispatch/dispatch.dart'; import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart' show SignInPayload, SignUpPayload, UserProfile; import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart'; -class AuthRepository { +class AuthService { Future> signIn({required String? email, required String? password}) { // final request = SignInPayload.create() diff --git a/frontend/app_flowy/lib/user/application/sign_in_bloc.dart b/frontend/app_flowy/lib/user/application/sign_in_bloc.dart index 3bba8b3217..25aa66cc50 100644 --- a/frontend/app_flowy/lib/user/application/sign_in_bloc.dart +++ b/frontend/app_flowy/lib/user/application/sign_in_bloc.dart @@ -1,4 +1,4 @@ -import 'package:app_flowy/user/infrastructure/repos/auth_repo.dart'; +import 'package:app_flowy/user/application/auth_service.dart'; import 'package:dartz/dartz.dart'; import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart'; import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart' show UserProfile, ErrorCode; @@ -8,8 +8,8 @@ import 'package:flutter_bloc/flutter_bloc.dart'; part 'sign_in_bloc.freezed.dart'; class SignInBloc extends Bloc { - final AuthRepository authRepo; - SignInBloc(this.authRepo) : super(SignInState.initial()) { + final AuthService authService; + SignInBloc(this.authService) : super(SignInState.initial()) { on((event, emit) async { await event.map( signedInWithUserEmailAndPassword: (e) async { @@ -31,7 +31,7 @@ class SignInBloc extends Bloc { Future _performActionOnSignIn(SignInState state, Emitter emit) async { emit(state.copyWith(isSubmitting: true, emailError: none(), passwordError: none(), successOrFail: none())); - final result = await authRepo.signIn( + final result = await authService.signIn( email: state.email, password: state.password, ); diff --git a/frontend/app_flowy/lib/user/application/sign_up_bloc.dart b/frontend/app_flowy/lib/user/application/sign_up_bloc.dart index af1f028741..11acd1d385 100644 --- a/frontend/app_flowy/lib/user/application/sign_up_bloc.dart +++ b/frontend/app_flowy/lib/user/application/sign_up_bloc.dart @@ -1,4 +1,4 @@ -import 'package:app_flowy/user/infrastructure/repos/auth_repo.dart'; +import 'package:app_flowy/user/application/auth_service.dart'; import 'package:dartz/dartz.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart' show UserProfile, ErrorCode; @@ -10,8 +10,8 @@ import 'package:app_flowy/generated/locale_keys.g.dart'; part 'sign_up_bloc.freezed.dart'; class SignUpBloc extends Bloc { - final AuthRepository autoRepo; - SignUpBloc(this.autoRepo) : super(SignUpState.initial()) { + final AuthService authService; + SignUpBloc(this.authService) : super(SignUpState.initial()) { on((event, emit) async { await event.map(signUpWithUserEmailAndPassword: (e) async { await _performActionOnSignUp(emit); @@ -62,7 +62,7 @@ class SignUpBloc extends Bloc { repeatPasswordError: none(), )); - final result = await autoRepo.signUp( + final result = await authService.signUp( name: state.email, password: state.password, email: state.email, diff --git a/frontend/app_flowy/lib/user/infrastructure/deps_resolver.dart b/frontend/app_flowy/lib/user/infrastructure/deps_resolver.dart index 9f6e6aadc6..b2d83a6f3d 100644 --- a/frontend/app_flowy/lib/user/infrastructure/deps_resolver.dart +++ b/frontend/app_flowy/lib/user/infrastructure/deps_resolver.dart @@ -1,7 +1,7 @@ +import 'package:app_flowy/user/application/auth_service.dart'; import 'package:app_flowy/user/application/sign_in_bloc.dart'; import 'package:app_flowy/user/application/sign_up_bloc.dart'; import 'package:app_flowy/user/application/splash_bloc.dart'; -import 'package:app_flowy/user/infrastructure/repos/auth_repo.dart'; import 'package:app_flowy/user/infrastructure/router.dart'; import 'package:app_flowy/workspace/application/edit_pannel/edit_pannel_bloc.dart'; import 'package:app_flowy/workspace/application/home/home_bloc.dart'; @@ -11,14 +11,14 @@ import 'network_monitor.dart'; class UserDepsResolver { static Future resolve(GetIt getIt) async { - getIt.registerFactory(() => AuthRepository()); + getIt.registerFactory(() => AuthService()); //Interface implementation getIt.registerFactory(() => AuthRouter()); //Bloc - getIt.registerFactory(() => SignInBloc(getIt())); - getIt.registerFactory(() => SignUpBloc(getIt())); + getIt.registerFactory(() => SignInBloc(getIt())); + getIt.registerFactory(() => SignUpBloc(getIt())); getIt.registerFactory(() => SplashRoute()); getIt.registerFactory(() => HomeBloc()); diff --git a/frontend/app_flowy/lib/user/infrastructure/router.dart b/frontend/app_flowy/lib/user/infrastructure/router.dart index 2a54e6c059..efe03b56f9 100644 --- a/frontend/app_flowy/lib/user/infrastructure/router.dart +++ b/frontend/app_flowy/lib/user/infrastructure/router.dart @@ -1,5 +1,5 @@ import 'package:app_flowy/startup/startup.dart'; -import 'package:app_flowy/user/infrastructure/repos/auth_repo.dart'; +import 'package:app_flowy/user/application/auth_service.dart'; import 'package:app_flowy/user/presentation/sign_in_screen.dart'; import 'package:app_flowy/user/presentation/sign_up_screen.dart'; import 'package:app_flowy/user/presentation/skip_log_in_screen.dart'; @@ -69,7 +69,7 @@ class SplashRoute { PageRoutes.fade( () => SkipLogInScreen( router: getIt(), - authRepo: getIt(), + authService: getIt(), ), RouteDurations.slow.inMilliseconds * .001), ); diff --git a/frontend/app_flowy/lib/user/presentation/skip_log_in_screen.dart b/frontend/app_flowy/lib/user/presentation/skip_log_in_screen.dart index bff30b8521..ab608a5bf8 100644 --- a/frontend/app_flowy/lib/user/presentation/skip_log_in_screen.dart +++ b/frontend/app_flowy/lib/user/presentation/skip_log_in_screen.dart @@ -1,6 +1,6 @@ +import 'package:app_flowy/user/application/auth_service.dart'; import 'package:app_flowy/user/application/user_listener.dart'; import 'package:app_flowy/user/infrastructure/router.dart'; -import 'package:app_flowy/user/infrastructure/repos/auth_repo.dart'; import 'package:app_flowy/user/presentation/widgets/background.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flowy_infra/size.dart'; @@ -21,12 +21,12 @@ import 'package:app_flowy/generated/locale_keys.g.dart'; class SkipLogInScreen extends StatefulWidget { final AuthRouter router; - final AuthRepository authRepo; + final AuthService authService; const SkipLogInScreen({ Key? key, required this.router, - required this.authRepo, + required this.authService, }) : super(key: key); @override @@ -99,7 +99,7 @@ class _SkipLogInScreenState extends State { const password = "AppFlowy123@"; final uid = uuid(); final userEmail = "$uid@appflowy.io"; - final result = await widget.authRepo.signUp( + final result = await widget.authService.signUp( name: LocaleKeys.defaultUsername.tr(), password: password, email: userEmail,