Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
refactor: put Auth business logic in a service
MikeWallaceDev33c758e71190
diff --git a/frontend/app_flowy/lib/user/infrastructure/repos/auth_repo.dart b/frontend/app_flowy/lib/user/application/auth_service.dartsimilarity index 98%rename from frontend/app_flowy/lib/user/infrastructure/repos/auth_repo.dartrename to frontend/app_flowy/lib/user/application/auth_service.dartindex 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<Either<UserProfile, FlowyError>> 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.dartindex 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<SignInEvent, SignInState> {- final AuthRepository authRepo;- SignInBloc(this.authRepo) : super(SignInState.initial()) {+ final AuthService authService;+ SignInBloc(this.authService) : super(SignInState.initial()) {on<SignInEvent>((event, emit) async {await event.map(signedInWithUserEmailAndPassword: (e) async {@@ -31,7 +31,7 @@ class SignInBloc extends Bloc<SignInEvent, SignInState> {Future<void> _performActionOnSignIn(SignInState state, Emitter<SignInState> 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.dartindex 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<SignUpEvent, SignUpState> {- final AuthRepository autoRepo;- SignUpBloc(this.autoRepo) : super(SignUpState.initial()) {+ final AuthService authService;+ SignUpBloc(this.authService) : super(SignUpState.initial()) {on<SignUpEvent>((event, emit) async {await event.map(signUpWithUserEmailAndPassword: (e) async {await _performActionOnSignUp(emit);@@ -62,7 +62,7 @@ class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {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.dartindex 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<void> resolve(GetIt getIt) async {- getIt.registerFactory<AuthRepository>(() => AuthRepository());+ getIt.registerFactory<AuthService>(() => AuthService());//Interface implementationgetIt.registerFactory<AuthRouter>(() => AuthRouter());//Bloc- getIt.registerFactory<SignInBloc>(() => SignInBloc(getIt<AuthRepository>()));- getIt.registerFactory<SignUpBloc>(() => SignUpBloc(getIt<AuthRepository>()));+ getIt.registerFactory<SignInBloc>(() => SignInBloc(getIt<AuthService>()));+ getIt.registerFactory<SignUpBloc>(() => SignUpBloc(getIt<AuthService>()));getIt.registerFactory<SplashRoute>(() => SplashRoute());getIt.registerFactory<HomeBloc>(() => HomeBloc());diff --git a/frontend/app_flowy/lib/user/infrastructure/router.dart b/frontend/app_flowy/lib/user/infrastructure/router.dartindex 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<AuthRouter>(),- authRepo: getIt<AuthRepository>(),+ authService: getIt<AuthService>(),),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.dartindex 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<SkipLogInScreen> {const password = "AppFlowy123@";final uid = uuid();final userEmail = "[email protected]";- final result = await widget.authRepo.signUp(+ final result = await widget.authService.signUp(name: LocaleKeys.defaultUsername.tr(),password: password,email: userEmail,