From c0a91d64377609c3cf9731302b208a6766f86b16 Mon Sep 17 00:00:00 2001 From: Hugo Pointcheval Date: Thu, 10 Nov 2022 13:30:13 -0500 Subject: [PATCH] feat!(auth): use form repository --- .../lib/src/core/constants/form_field.dart | 20 +++ .../lib/src/core/constants/form_name.dart | 20 +++ .../authentication_repository_impl.dart | 119 +++++++++++++----- .../authentication_repository.dart | 16 +-- 4 files changed, 136 insertions(+), 39 deletions(-) create mode 100644 packages/wyatt_authentication_bloc/lib/src/core/constants/form_field.dart create mode 100644 packages/wyatt_authentication_bloc/lib/src/core/constants/form_name.dart diff --git a/packages/wyatt_authentication_bloc/lib/src/core/constants/form_field.dart b/packages/wyatt_authentication_bloc/lib/src/core/constants/form_field.dart new file mode 100644 index 00000000..295cfc2f --- /dev/null +++ b/packages/wyatt_authentication_bloc/lib/src/core/constants/form_field.dart @@ -0,0 +1,20 @@ +// 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 . + +abstract class AuthFormField { + static const email = 'wyattEmailField'; + static const password = 'wyattPasswordField'; +} diff --git a/packages/wyatt_authentication_bloc/lib/src/core/constants/form_name.dart b/packages/wyatt_authentication_bloc/lib/src/core/constants/form_name.dart new file mode 100644 index 00000000..5a788715 --- /dev/null +++ b/packages/wyatt_authentication_bloc/lib/src/core/constants/form_name.dart @@ -0,0 +1,20 @@ +// 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 . + +abstract class AuthFormName { + static const String signUpForm = 'wyattSignUpForm'; + static const String signInForm = 'wyattSignInForm'; +} diff --git a/packages/wyatt_authentication_bloc/lib/src/data/repositories/authentication_repository_impl.dart b/packages/wyatt_authentication_bloc/lib/src/data/repositories/authentication_repository_impl.dart index e5526ea8..00bf067c 100644 --- a/packages/wyatt_authentication_bloc/lib/src/data/repositories/authentication_repository_impl.dart +++ b/packages/wyatt_authentication_bloc/lib/src/data/repositories/authentication_repository_impl.dart @@ -15,28 +15,75 @@ // along with this program. If not, see . import 'package:wyatt_architecture/wyatt_architecture.dart'; +import 'package:wyatt_authentication_bloc/src/core/constants/form_field.dart'; +import 'package:wyatt_authentication_bloc/src/core/constants/form_name.dart'; import 'package:wyatt_authentication_bloc/src/data/models/account_wrapper_model.dart'; -import 'package:wyatt_authentication_bloc/src/domain/data_sources/local/authentication_local_data_source.dart'; +import 'package:wyatt_authentication_bloc/src/domain/data_sources/local/authentication_cache_data_source.dart'; import 'package:wyatt_authentication_bloc/src/domain/data_sources/remote/authentication_remote_data_source.dart'; import 'package:wyatt_authentication_bloc/src/domain/entities/account.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_form_bloc/wyatt_form_bloc.dart'; import 'package:wyatt_type_utils/wyatt_type_utils.dart'; +typedef OnSignUpSuccess = FutureResult Function( + Account? account, + WyattForm form, +); + +typedef OnAuthChange = FutureResult Function(Account? account); + class AuthenticationRepositoryImpl extends AuthenticationRepository { - final AuthenticationLocalDataSource _authenticationLocalDataSource; + final AuthenticationCacheDataSource _authenticationLocalDataSource; final AuthenticationRemoteDataSource _authenticationRemoteDataSource; - final FutureResult Function(Account? account)? _onSignUpSuccess; - final FutureResult Function(Account? account)? _onAccountChanges; + late FormRepository _formRepository; - AuthenticationRepositoryImpl( - this._authenticationLocalDataSource, - this._authenticationRemoteDataSource, - this._onSignUpSuccess, - this._onAccountChanges, - ); + final OnSignUpSuccess? _onSignUpSuccess; + + final OnAuthChange? _onAccountChanges; + + AuthenticationRepositoryImpl({ + required AuthenticationCacheDataSource authenticationCacheDataSource, + required AuthenticationRemoteDataSource authenticationRemoteDataSource, + FormRepository? formRepository, + // ignore: strict_raw_type + List? extraSignUpInputs, + OnSignUpSuccess? onSignUpSuccess, + OnAuthChange? onAuthChange, + }) : _authenticationLocalDataSource = authenticationCacheDataSource, + _authenticationRemoteDataSource = authenticationRemoteDataSource, + _onSignUpSuccess = onSignUpSuccess, + _onAccountChanges = onAuthChange { + _formRepository = formRepository ?? FormRepositoryImpl(); + if (formRepository != null) { + return; + } + _formRepository + ..registerForm( + WyattFormImpl( + [ + FormInput(AuthFormField.email, const Email.pure()), + FormInput(AuthFormField.password, const Password.pure()) + ], + name: AuthFormName.signInForm, + ), + ) + ..registerForm( + WyattFormImpl( + [ + FormInput(AuthFormField.email, const Email.pure()), + FormInput(AuthFormField.password, const Password.pure()), + ...extraSignUpInputs ?? [] + ], + name: AuthFormName.signUpForm, + ), + ); + } + + @override + FormRepository get formRepository => _formRepository; @override FutureResult signInWithEmailAndPassword({ @@ -50,7 +97,7 @@ class AuthenticationRepositoryImpl email: email, password: password, ); - _authenticationLocalDataSource.storeAccount(account); + await _authenticationLocalDataSource.storeAccount(account); return account; }, (error) => error, @@ -61,7 +108,7 @@ class AuthenticationRepositoryImpl Result.tryCatchAsync( () async { await _authenticationRemoteDataSource.signOut(); - _authenticationLocalDataSource.destroy(); + await _authenticationLocalDataSource.destroy(); }, (error) => error, ); @@ -77,10 +124,13 @@ class AuthenticationRepositoryImpl email: email, password: password, ); - _authenticationLocalDataSource.storeAccount(account); + await _authenticationLocalDataSource.storeAccount(account); if (_onSignUpSuccess.isNotNull) { - final dataResult = await _onSignUpSuccess!.call(account); - dataResult.fold( + final dataResult = await _onSignUpSuccess!.call( + account, + _formRepository.accessForm(AuthFormName.signUpForm).clone(), + ); + await dataResult.foldAsync( _authenticationLocalDataSource.storeData, (error) => throw error, ); @@ -91,46 +141,51 @@ class AuthenticationRepositoryImpl ); @override - Result destroyCache() => - Result.tryCatch( + FutureResult destroyCache() => + Result.tryCatchAsync( _authenticationLocalDataSource.destroy, (error) => error, ); @override - Result getAccount() => - Result.tryCatch( + FutureResult> getCache() => + Result.tryCatchAsync, AppException, AppException>( + _authenticationLocalDataSource.load, + (error) => error, + ); + + @override + FutureResult getAccount() => + Result.tryCatchAsync( _authenticationLocalDataSource.loadAccount, (error) => error, ); @override - Result setAccount( + FutureResult setAccount( Account account, ) => - Result.tryCatch( - () { - _authenticationLocalDataSource.storeAccount(account); - return account; + Result.tryCatchAsync( + () async { + await _authenticationLocalDataSource.storeAccount(account); }, (error) => error, ); @override - Result getData() => - Result.tryCatch( + FutureResult getData() => + Result.tryCatchAsync( _authenticationLocalDataSource.loadData, (error) => error, ); @override - Result setData( - T data, + FutureResult setData( + T? data, ) => - Result.tryCatch( - () { - _authenticationLocalDataSource.storeData(data); - return data; + Result.tryCatchAsync( + () async { + await _authenticationLocalDataSource.storeData(data); }, (error) => error, ); diff --git a/packages/wyatt_authentication_bloc/lib/src/domain/repositories/authentication_repository.dart b/packages/wyatt_authentication_bloc/lib/src/domain/repositories/authentication_repository.dart index 432b1de6..cece19c9 100644 --- a/packages/wyatt_authentication_bloc/lib/src/domain/repositories/authentication_repository.dart +++ b/packages/wyatt_authentication_bloc/lib/src/domain/repositories/authentication_repository.dart @@ -17,13 +17,14 @@ import 'package:wyatt_architecture/wyatt_architecture.dart'; import 'package:wyatt_authentication_bloc/src/domain/entities/account.dart'; import 'package:wyatt_authentication_bloc/src/domain/entities/account_wrapper.dart'; -import 'package:wyatt_type_utils/wyatt_type_utils.dart'; +import 'package:wyatt_form_bloc/wyatt_form_bloc.dart'; abstract class AuthenticationRepository extends BaseRepository { + FormRepository get formRepository; + FutureResult signUp({ required String email, required String password, - }); FutureResult signInWithEmailAndPassword({ @@ -37,11 +38,12 @@ abstract class AuthenticationRepository extends BaseRepository { FutureResult getIdentityToken(); - Result getAccount(); - Result setAccount(Account account); + FutureResult getAccount(); + FutureResult setAccount(Account account); - Result getData(); - Result setData(T data); + FutureResult getData(); + FutureResult setData(T? data); - Result destroyCache(); + FutureResult> getCache(); + FutureResult destroyCache(); }