feat!(auth): use form repository
This commit is contained in:
parent
59bd3edb0d
commit
c0a91d6437
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
abstract class AuthFormField {
|
||||
static const email = 'wyattEmailField';
|
||||
static const password = 'wyattPasswordField';
|
||||
}
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
abstract class AuthFormName {
|
||||
static const String signUpForm = 'wyattSignUpForm';
|
||||
static const String signInForm = 'wyattSignInForm';
|
||||
}
|
@ -15,28 +15,75 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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<T> = FutureResult<T?> Function(
|
||||
Account? account,
|
||||
WyattForm form,
|
||||
);
|
||||
|
||||
typedef OnAuthChange<T> = FutureResult<T?> Function(Account? account);
|
||||
|
||||
class AuthenticationRepositoryImpl<T extends Object>
|
||||
extends AuthenticationRepository<T> {
|
||||
final AuthenticationLocalDataSource<T> _authenticationLocalDataSource;
|
||||
final AuthenticationCacheDataSource<T> _authenticationLocalDataSource;
|
||||
final AuthenticationRemoteDataSource _authenticationRemoteDataSource;
|
||||
|
||||
final FutureResult<T?> Function(Account? account)? _onSignUpSuccess;
|
||||
final FutureResult<T?> Function(Account? account)? _onAccountChanges;
|
||||
late FormRepository _formRepository;
|
||||
|
||||
AuthenticationRepositoryImpl(
|
||||
this._authenticationLocalDataSource,
|
||||
this._authenticationRemoteDataSource,
|
||||
this._onSignUpSuccess,
|
||||
this._onAccountChanges,
|
||||
);
|
||||
final OnSignUpSuccess<T>? _onSignUpSuccess;
|
||||
|
||||
final OnAuthChange<T>? _onAccountChanges;
|
||||
|
||||
AuthenticationRepositoryImpl({
|
||||
required AuthenticationCacheDataSource<T> authenticationCacheDataSource,
|
||||
required AuthenticationRemoteDataSource authenticationRemoteDataSource,
|
||||
FormRepository? formRepository,
|
||||
// ignore: strict_raw_type
|
||||
List<FormInput>? extraSignUpInputs,
|
||||
OnSignUpSuccess<T>? onSignUpSuccess,
|
||||
OnAuthChange<T>? 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<Account> signInWithEmailAndPassword({
|
||||
@ -50,7 +97,7 @@ class AuthenticationRepositoryImpl<T extends Object>
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
_authenticationLocalDataSource.storeAccount(account);
|
||||
await _authenticationLocalDataSource.storeAccount(account);
|
||||
return account;
|
||||
},
|
||||
(error) => error,
|
||||
@ -61,7 +108,7 @@ class AuthenticationRepositoryImpl<T extends Object>
|
||||
Result.tryCatchAsync<void, AppException, AppException>(
|
||||
() async {
|
||||
await _authenticationRemoteDataSource.signOut();
|
||||
_authenticationLocalDataSource.destroy();
|
||||
await _authenticationLocalDataSource.destroy();
|
||||
},
|
||||
(error) => error,
|
||||
);
|
||||
@ -77,10 +124,13 @@ class AuthenticationRepositoryImpl<T extends Object>
|
||||
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<T extends Object>
|
||||
);
|
||||
|
||||
@override
|
||||
Result<void, AppException> destroyCache() =>
|
||||
Result.tryCatch<void, AppException, AppException>(
|
||||
FutureResult<void> destroyCache() =>
|
||||
Result.tryCatchAsync<void, AppException, AppException>(
|
||||
_authenticationLocalDataSource.destroy,
|
||||
(error) => error,
|
||||
);
|
||||
|
||||
@override
|
||||
Result<Account, AppException> getAccount() =>
|
||||
Result.tryCatch<Account, AppException, AppException>(
|
||||
FutureResult<AccountWrapper<T>> getCache() =>
|
||||
Result.tryCatchAsync<AccountWrapper<T>, AppException, AppException>(
|
||||
_authenticationLocalDataSource.load,
|
||||
(error) => error,
|
||||
);
|
||||
|
||||
@override
|
||||
FutureResult<Account> getAccount() =>
|
||||
Result.tryCatchAsync<Account, AppException, AppException>(
|
||||
_authenticationLocalDataSource.loadAccount,
|
||||
(error) => error,
|
||||
);
|
||||
|
||||
@override
|
||||
Result<Account, AppException> setAccount(
|
||||
FutureResult<void> setAccount(
|
||||
Account account,
|
||||
) =>
|
||||
Result.tryCatch<Account, AppException, AppException>(
|
||||
() {
|
||||
_authenticationLocalDataSource.storeAccount(account);
|
||||
return account;
|
||||
Result.tryCatchAsync<void, AppException, AppException>(
|
||||
() async {
|
||||
await _authenticationLocalDataSource.storeAccount(account);
|
||||
},
|
||||
(error) => error,
|
||||
);
|
||||
|
||||
@override
|
||||
Result<T, AppException> getData() =>
|
||||
Result.tryCatch<T, AppException, AppException>(
|
||||
FutureResult<T> getData() =>
|
||||
Result.tryCatchAsync<T, AppException, AppException>(
|
||||
_authenticationLocalDataSource.loadData,
|
||||
(error) => error,
|
||||
);
|
||||
|
||||
@override
|
||||
Result<T, AppException> setData(
|
||||
T data,
|
||||
FutureResult<void> setData(
|
||||
T? data,
|
||||
) =>
|
||||
Result.tryCatch<T, AppException, AppException>(
|
||||
() {
|
||||
_authenticationLocalDataSource.storeData(data);
|
||||
return data;
|
||||
Result.tryCatchAsync<void, AppException, AppException>(
|
||||
() async {
|
||||
await _authenticationLocalDataSource.storeData(data);
|
||||
},
|
||||
(error) => error,
|
||||
);
|
||||
|
@ -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<T> extends BaseRepository {
|
||||
FormRepository get formRepository;
|
||||
|
||||
FutureResult<Account> signUp({
|
||||
required String email,
|
||||
required String password,
|
||||
|
||||
});
|
||||
|
||||
FutureResult<Account> signInWithEmailAndPassword({
|
||||
@ -37,11 +38,12 @@ abstract class AuthenticationRepository<T> extends BaseRepository {
|
||||
|
||||
FutureResult<String> getIdentityToken();
|
||||
|
||||
Result<Account, AppException> getAccount();
|
||||
Result<Account, AppException> setAccount(Account account);
|
||||
FutureResult<Account> getAccount();
|
||||
FutureResult<void> setAccount(Account account);
|
||||
|
||||
Result<T, AppException> getData();
|
||||
Result<T, AppException> setData(T data);
|
||||
FutureResult<T> getData();
|
||||
FutureResult<void> setData(T? data);
|
||||
|
||||
Result<void, AppException> destroyCache();
|
||||
FutureResult<AccountWrapper<T>> getCache();
|
||||
FutureResult<void> destroyCache();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user