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/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import 'package:wyatt_architecture/wyatt_architecture.dart';
|
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/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/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.dart';
|
||||||
import 'package:wyatt_authentication_bloc/src/domain/entities/account_wrapper.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_form_bloc/wyatt_form_bloc.dart';
|
||||||
import 'package:wyatt_type_utils/wyatt_type_utils.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>
|
class AuthenticationRepositoryImpl<T extends Object>
|
||||||
extends AuthenticationRepository<T> {
|
extends AuthenticationRepository<T> {
|
||||||
final AuthenticationLocalDataSource<T> _authenticationLocalDataSource;
|
final AuthenticationCacheDataSource<T> _authenticationLocalDataSource;
|
||||||
final AuthenticationRemoteDataSource _authenticationRemoteDataSource;
|
final AuthenticationRemoteDataSource _authenticationRemoteDataSource;
|
||||||
|
|
||||||
final FutureResult<T?> Function(Account? account)? _onSignUpSuccess;
|
late FormRepository _formRepository;
|
||||||
final FutureResult<T?> Function(Account? account)? _onAccountChanges;
|
|
||||||
|
|
||||||
AuthenticationRepositoryImpl(
|
final OnSignUpSuccess<T>? _onSignUpSuccess;
|
||||||
this._authenticationLocalDataSource,
|
|
||||||
this._authenticationRemoteDataSource,
|
final OnAuthChange<T>? _onAccountChanges;
|
||||||
this._onSignUpSuccess,
|
|
||||||
this._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
|
@override
|
||||||
FutureResult<Account> signInWithEmailAndPassword({
|
FutureResult<Account> signInWithEmailAndPassword({
|
||||||
@ -50,7 +97,7 @@ class AuthenticationRepositoryImpl<T extends Object>
|
|||||||
email: email,
|
email: email,
|
||||||
password: password,
|
password: password,
|
||||||
);
|
);
|
||||||
_authenticationLocalDataSource.storeAccount(account);
|
await _authenticationLocalDataSource.storeAccount(account);
|
||||||
return account;
|
return account;
|
||||||
},
|
},
|
||||||
(error) => error,
|
(error) => error,
|
||||||
@ -61,7 +108,7 @@ class AuthenticationRepositoryImpl<T extends Object>
|
|||||||
Result.tryCatchAsync<void, AppException, AppException>(
|
Result.tryCatchAsync<void, AppException, AppException>(
|
||||||
() async {
|
() async {
|
||||||
await _authenticationRemoteDataSource.signOut();
|
await _authenticationRemoteDataSource.signOut();
|
||||||
_authenticationLocalDataSource.destroy();
|
await _authenticationLocalDataSource.destroy();
|
||||||
},
|
},
|
||||||
(error) => error,
|
(error) => error,
|
||||||
);
|
);
|
||||||
@ -77,10 +124,13 @@ class AuthenticationRepositoryImpl<T extends Object>
|
|||||||
email: email,
|
email: email,
|
||||||
password: password,
|
password: password,
|
||||||
);
|
);
|
||||||
_authenticationLocalDataSource.storeAccount(account);
|
await _authenticationLocalDataSource.storeAccount(account);
|
||||||
if (_onSignUpSuccess.isNotNull) {
|
if (_onSignUpSuccess.isNotNull) {
|
||||||
final dataResult = await _onSignUpSuccess!.call(account);
|
final dataResult = await _onSignUpSuccess!.call(
|
||||||
dataResult.fold(
|
account,
|
||||||
|
_formRepository.accessForm(AuthFormName.signUpForm).clone(),
|
||||||
|
);
|
||||||
|
await dataResult.foldAsync(
|
||||||
_authenticationLocalDataSource.storeData,
|
_authenticationLocalDataSource.storeData,
|
||||||
(error) => throw error,
|
(error) => throw error,
|
||||||
);
|
);
|
||||||
@ -91,46 +141,51 @@ class AuthenticationRepositoryImpl<T extends Object>
|
|||||||
);
|
);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Result<void, AppException> destroyCache() =>
|
FutureResult<void> destroyCache() =>
|
||||||
Result.tryCatch<void, AppException, AppException>(
|
Result.tryCatchAsync<void, AppException, AppException>(
|
||||||
_authenticationLocalDataSource.destroy,
|
_authenticationLocalDataSource.destroy,
|
||||||
(error) => error,
|
(error) => error,
|
||||||
);
|
);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Result<Account, AppException> getAccount() =>
|
FutureResult<AccountWrapper<T>> getCache() =>
|
||||||
Result.tryCatch<Account, AppException, AppException>(
|
Result.tryCatchAsync<AccountWrapper<T>, AppException, AppException>(
|
||||||
|
_authenticationLocalDataSource.load,
|
||||||
|
(error) => error,
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
FutureResult<Account> getAccount() =>
|
||||||
|
Result.tryCatchAsync<Account, AppException, AppException>(
|
||||||
_authenticationLocalDataSource.loadAccount,
|
_authenticationLocalDataSource.loadAccount,
|
||||||
(error) => error,
|
(error) => error,
|
||||||
);
|
);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Result<Account, AppException> setAccount(
|
FutureResult<void> setAccount(
|
||||||
Account account,
|
Account account,
|
||||||
) =>
|
) =>
|
||||||
Result.tryCatch<Account, AppException, AppException>(
|
Result.tryCatchAsync<void, AppException, AppException>(
|
||||||
() {
|
() async {
|
||||||
_authenticationLocalDataSource.storeAccount(account);
|
await _authenticationLocalDataSource.storeAccount(account);
|
||||||
return account;
|
|
||||||
},
|
},
|
||||||
(error) => error,
|
(error) => error,
|
||||||
);
|
);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Result<T, AppException> getData() =>
|
FutureResult<T> getData() =>
|
||||||
Result.tryCatch<T, AppException, AppException>(
|
Result.tryCatchAsync<T, AppException, AppException>(
|
||||||
_authenticationLocalDataSource.loadData,
|
_authenticationLocalDataSource.loadData,
|
||||||
(error) => error,
|
(error) => error,
|
||||||
);
|
);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Result<T, AppException> setData(
|
FutureResult<void> setData(
|
||||||
T data,
|
T? data,
|
||||||
) =>
|
) =>
|
||||||
Result.tryCatch<T, AppException, AppException>(
|
Result.tryCatchAsync<void, AppException, AppException>(
|
||||||
() {
|
() async {
|
||||||
_authenticationLocalDataSource.storeData(data);
|
await _authenticationLocalDataSource.storeData(data);
|
||||||
return data;
|
|
||||||
},
|
},
|
||||||
(error) => error,
|
(error) => error,
|
||||||
);
|
);
|
||||||
|
@ -17,13 +17,14 @@
|
|||||||
import 'package:wyatt_architecture/wyatt_architecture.dart';
|
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.dart';
|
||||||
import 'package:wyatt_authentication_bloc/src/domain/entities/account_wrapper.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 {
|
abstract class AuthenticationRepository<T> extends BaseRepository {
|
||||||
|
FormRepository get formRepository;
|
||||||
|
|
||||||
FutureResult<Account> signUp({
|
FutureResult<Account> signUp({
|
||||||
required String email,
|
required String email,
|
||||||
required String password,
|
required String password,
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
FutureResult<Account> signInWithEmailAndPassword({
|
FutureResult<Account> signInWithEmailAndPassword({
|
||||||
@ -37,11 +38,12 @@ abstract class AuthenticationRepository<T> extends BaseRepository {
|
|||||||
|
|
||||||
FutureResult<String> getIdentityToken();
|
FutureResult<String> getIdentityToken();
|
||||||
|
|
||||||
Result<Account, AppException> getAccount();
|
FutureResult<Account> getAccount();
|
||||||
Result<Account, AppException> setAccount(Account account);
|
FutureResult<void> setAccount(Account account);
|
||||||
|
|
||||||
Result<T, AppException> getData();
|
FutureResult<T> getData();
|
||||||
Result<T, AppException> setData(T data);
|
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