refactor: make signup/in, passwordreset cubit extendable
This commit is contained in:
parent
222b650bd2
commit
fe4ce3cbbc
@ -91,6 +91,17 @@ class AuthenticationRepositoryImpl<T extends Object>
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
),
|
||||
)
|
||||
..registerForm(
|
||||
WyattFormImpl(
|
||||
[
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
customEmailValidator ?? const Email.pure(),
|
||||
),
|
||||
],
|
||||
name: AuthFormName.passwordResetForm,
|
||||
),
|
||||
);
|
||||
}
|
||||
final AuthenticationCacheDataSource<T> _authenticationLocalDataSource;
|
||||
|
@ -27,27 +27,26 @@ part 'authentication_state.dart';
|
||||
|
||||
class AuthenticationCubit<Extra> extends Cubit<AuthenticationState<Extra>> {
|
||||
AuthenticationCubit({
|
||||
required AuthenticationRepository<Extra> authenticationRepository,
|
||||
}) : _authenticationRepository = authenticationRepository,
|
||||
super(const AuthenticationState.unknown()) {
|
||||
required this.authenticationRepository,
|
||||
}) : super(const AuthenticationState.unknown()) {
|
||||
_listenForAuthenticationChanges();
|
||||
}
|
||||
final AuthenticationRepository<Extra> _authenticationRepository;
|
||||
final AuthenticationRepository<Extra> authenticationRepository;
|
||||
|
||||
void _listenForAuthenticationChanges() {
|
||||
_authenticationRepository.streamAccount().listen((accountFutureResult) {
|
||||
authenticationRepository.streamAccount().listen((accountFutureResult) {
|
||||
accountFutureResult.fold(
|
||||
(value) {
|
||||
if (value.account.isNotNull) {
|
||||
emit(AuthenticationState<Extra>.authenticated(value));
|
||||
return;
|
||||
}
|
||||
_authenticationRepository.destroyCache();
|
||||
authenticationRepository.destroyCache();
|
||||
emit(AuthenticationState<Extra>.unauthenticated());
|
||||
return;
|
||||
},
|
||||
(error) {
|
||||
_authenticationRepository.destroyCache();
|
||||
authenticationRepository.destroyCache();
|
||||
emit(AuthenticationState<Extra>.unauthenticated());
|
||||
return;
|
||||
},
|
||||
@ -58,7 +57,7 @@ class AuthenticationCubit<Extra> extends Cubit<AuthenticationState<Extra>> {
|
||||
/// If authenticated, re-emits state with data freshly loaded from cache.
|
||||
FutureOr<void> reloadCache() async {
|
||||
if (state.status == AuthenticationStatus.authenticated) {
|
||||
final data = await _authenticationRepository.getCache();
|
||||
final data = await authenticationRepository.getCache();
|
||||
emit(
|
||||
data.fold(
|
||||
AuthenticationState<Extra>.authenticated,
|
||||
@ -69,7 +68,6 @@ class AuthenticationCubit<Extra> extends Cubit<AuthenticationState<Extra>> {
|
||||
}
|
||||
|
||||
FutureOr<void> signOut() {
|
||||
// TODO(hpcl): maybe force unauthenticated by emitting an event
|
||||
_authenticationRepository.signOut();
|
||||
authenticationRepository.signOut();
|
||||
}
|
||||
}
|
||||
|
@ -25,14 +25,14 @@ part 'email_verification_state.dart';
|
||||
|
||||
class EmailVerificationCubit<Extra> extends Cubit<EmailVerificationState> {
|
||||
EmailVerificationCubit({
|
||||
required AuthenticationRepository<Extra> authenticationRepository,
|
||||
}) : _authenticationRepository = authenticationRepository,
|
||||
super(const EmailVerificationState());
|
||||
final AuthenticationRepository<Extra> _authenticationRepository;
|
||||
required this.authenticationRepository,
|
||||
}) : super(const EmailVerificationState());
|
||||
|
||||
final AuthenticationRepository<Extra> authenticationRepository;
|
||||
|
||||
FutureOr<void> sendEmailVerification() async {
|
||||
emit(state.copyWith(status: FormStatus.submissionInProgress));
|
||||
final response = await _authenticationRepository.sendEmailVerification();
|
||||
final response = await authenticationRepository.sendEmailVerification();
|
||||
emit(
|
||||
response.fold(
|
||||
(value) => state.copyWith(status: FormStatus.submissionSuccess),
|
||||
@ -47,7 +47,7 @@ class EmailVerificationCubit<Extra> extends Cubit<EmailVerificationState> {
|
||||
FutureOr<void> checkEmailVerification() async {
|
||||
emit(state.copyWith(status: FormStatus.submissionInProgress));
|
||||
|
||||
final refresh = await _authenticationRepository.refresh();
|
||||
final refresh = await authenticationRepository.refresh();
|
||||
if (refresh.isErr) {
|
||||
final refreshError = refresh.err!;
|
||||
emit(
|
||||
@ -59,7 +59,7 @@ class EmailVerificationCubit<Extra> extends Cubit<EmailVerificationState> {
|
||||
return;
|
||||
}
|
||||
|
||||
final currentAccount = await _authenticationRepository.getAccount();
|
||||
final currentAccount = await authenticationRepository.getAccount();
|
||||
emit(
|
||||
currentAccount.fold(
|
||||
(value) => state.copyWith(
|
||||
|
@ -26,17 +26,17 @@ part 'password_reset_state.dart';
|
||||
|
||||
class PasswordResetCubit<Extra> extends FormDataCubit<PasswordResetState> {
|
||||
PasswordResetCubit({
|
||||
required AuthenticationRepository<Extra> authenticationRepository,
|
||||
}) : _authenticationRepository = authenticationRepository,
|
||||
required this.authenticationRepository,
|
||||
}) :
|
||||
super(
|
||||
PasswordResetState(
|
||||
form: authenticationRepository.formRepository
|
||||
.accessForm(AuthFormName.passwordResetForm),
|
||||
),
|
||||
);
|
||||
final AuthenticationRepository<Extra> _authenticationRepository;
|
||||
FormRepository get _formRepository =>
|
||||
_authenticationRepository.formRepository;
|
||||
final AuthenticationRepository<Extra> authenticationRepository;
|
||||
FormRepository get formRepository =>
|
||||
authenticationRepository.formRepository;
|
||||
|
||||
@override
|
||||
String get formName => AuthFormName.passwordResetForm;
|
||||
@ -51,11 +51,11 @@ class PasswordResetCubit<Extra> extends FormDataCubit<PasswordResetState> {
|
||||
String key,
|
||||
FormInputValidator<Value, ValidationError> dirtyValue,
|
||||
) {
|
||||
final form = _formRepository.accessForm(formName).clone();
|
||||
final form = formRepository.accessForm(formName).clone();
|
||||
|
||||
try {
|
||||
form.updateValidator(key, dirtyValue);
|
||||
_formRepository.updateForm(form);
|
||||
formRepository.updateForm(form);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
@ -68,7 +68,7 @@ class PasswordResetCubit<Extra> extends FormDataCubit<PasswordResetState> {
|
||||
@override
|
||||
FutureOr<void> reset() {
|
||||
final form = state.form.reset();
|
||||
_formRepository.updateForm(form);
|
||||
formRepository.updateForm(form);
|
||||
emit(
|
||||
state.copyWith(form: form, status: form.validate()),
|
||||
);
|
||||
@ -82,7 +82,7 @@ class PasswordResetCubit<Extra> extends FormDataCubit<PasswordResetState> {
|
||||
|
||||
emit(state.copyWith(status: FormStatus.submissionInProgress));
|
||||
|
||||
final form = _formRepository.accessForm(formName);
|
||||
final form = formRepository.accessForm(formName);
|
||||
final email = form.valueOf<String?>(AuthFormField.email);
|
||||
|
||||
if (email.isNullOrEmpty) {
|
||||
@ -94,7 +94,7 @@ class PasswordResetCubit<Extra> extends FormDataCubit<PasswordResetState> {
|
||||
);
|
||||
}
|
||||
|
||||
final response = await _authenticationRepository.sendPasswordResetEmail(
|
||||
final response = await authenticationRepository.sendPasswordResetEmail(
|
||||
email: email!,
|
||||
);
|
||||
|
||||
@ -114,9 +114,9 @@ class PasswordResetCubit<Extra> extends FormDataCubit<PasswordResetState> {
|
||||
WyattForm form, {
|
||||
SetOperation operation = SetOperation.replace,
|
||||
}) {
|
||||
final WyattForm current = _formRepository.accessForm(formName).clone();
|
||||
final WyattForm current = formRepository.accessForm(formName).clone();
|
||||
final WyattForm newForm = operation.operation.call(current, form);
|
||||
_formRepository.updateForm(newForm);
|
||||
formRepository.updateForm(newForm);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
@ -130,7 +130,7 @@ class PasswordResetCubit<Extra> extends FormDataCubit<PasswordResetState> {
|
||||
FutureOr<void> validate() {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: _formRepository.accessForm(formName).validate(),
|
||||
status: formRepository.accessForm(formName).validate(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
@ -24,23 +24,21 @@ part 'sign_in_state.dart';
|
||||
|
||||
class SignInCubit<Extra> extends FormDataCubit<SignInState> {
|
||||
SignInCubit({
|
||||
required AuthenticationRepository<Extra> authenticationRepository,
|
||||
}) : _authenticationRepository = authenticationRepository,
|
||||
super(
|
||||
required this.authenticationRepository,
|
||||
}) : super(
|
||||
SignInState(
|
||||
form: authenticationRepository.formRepository
|
||||
.accessForm(AuthFormName.signInForm),
|
||||
),
|
||||
);
|
||||
final AuthenticationRepository<Extra> _authenticationRepository;
|
||||
FormRepository get _formRepository =>
|
||||
_authenticationRepository.formRepository;
|
||||
final AuthenticationRepository<Extra> authenticationRepository;
|
||||
FormRepository get formRepository => authenticationRepository.formRepository;
|
||||
|
||||
@override
|
||||
String get formName => AuthFormName.signInForm;
|
||||
|
||||
void emailChanged(String value) {
|
||||
final emailValidatorType = _formRepository
|
||||
final emailValidatorType = formRepository
|
||||
.accessForm(formName)
|
||||
.validatorOf(AuthFormField.email)
|
||||
.runtimeType;
|
||||
@ -54,7 +52,7 @@ class SignInCubit<Extra> extends FormDataCubit<SignInState> {
|
||||
}
|
||||
|
||||
void passwordChanged(String value) {
|
||||
final passwordValidatorType = _formRepository
|
||||
final passwordValidatorType = formRepository
|
||||
.accessForm(formName)
|
||||
.validatorOf(AuthFormField.password)
|
||||
.runtimeType;
|
||||
@ -91,11 +89,11 @@ class SignInCubit<Extra> extends FormDataCubit<SignInState> {
|
||||
String key,
|
||||
FormInputValidator<Value, ValidationError> dirtyValue,
|
||||
) {
|
||||
final form = _formRepository.accessForm(formName).clone();
|
||||
final form = formRepository.accessForm(formName).clone();
|
||||
|
||||
try {
|
||||
form.updateValidator(key, dirtyValue);
|
||||
_formRepository.updateForm(form);
|
||||
formRepository.updateForm(form);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
@ -108,7 +106,7 @@ class SignInCubit<Extra> extends FormDataCubit<SignInState> {
|
||||
@override
|
||||
FutureOr<void> reset() {
|
||||
final form = state.form.reset();
|
||||
_formRepository.updateForm(form);
|
||||
formRepository.updateForm(form);
|
||||
emit(
|
||||
state.copyWith(form: form, status: form.validate()),
|
||||
);
|
||||
@ -125,9 +123,9 @@ class SignInCubit<Extra> extends FormDataCubit<SignInState> {
|
||||
WyattForm form, {
|
||||
SetOperation operation = SetOperation.replace,
|
||||
}) {
|
||||
final WyattForm current = _formRepository.accessForm(formName).clone();
|
||||
final WyattForm current = formRepository.accessForm(formName).clone();
|
||||
final WyattForm newForm = operation.operation.call(current, form);
|
||||
_formRepository.updateForm(newForm);
|
||||
formRepository.updateForm(newForm);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
@ -141,7 +139,7 @@ class SignInCubit<Extra> extends FormDataCubit<SignInState> {
|
||||
FutureOr<void> validate() {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: _formRepository.accessForm(formName).validate(),
|
||||
status: formRepository.accessForm(formName).validate(),
|
||||
),
|
||||
);
|
||||
}
|
||||
@ -157,7 +155,7 @@ class SignInCubit<Extra> extends FormDataCubit<SignInState> {
|
||||
|
||||
emit(state.copyWith(status: FormStatus.submissionInProgress));
|
||||
|
||||
final form = _formRepository.accessForm(formName);
|
||||
final form = formRepository.accessForm(formName);
|
||||
final email = form.valueOf<String?>(AuthFormField.email);
|
||||
final password = form.valueOf<String?>(AuthFormField.password);
|
||||
|
||||
@ -170,7 +168,7 @@ class SignInCubit<Extra> extends FormDataCubit<SignInState> {
|
||||
);
|
||||
}
|
||||
|
||||
final uid = await _authenticationRepository.signInWithEmailAndPassword(
|
||||
final uid = await authenticationRepository.signInWithEmailAndPassword(
|
||||
email: email!,
|
||||
password: password!,
|
||||
);
|
||||
@ -193,7 +191,7 @@ class SignInCubit<Extra> extends FormDataCubit<SignInState> {
|
||||
|
||||
emit(state.copyWith(status: FormStatus.submissionInProgress));
|
||||
|
||||
final uid = await _authenticationRepository.signInAnonymously();
|
||||
final uid = await authenticationRepository.signInAnonymously();
|
||||
|
||||
emit(
|
||||
uid.fold(
|
||||
@ -213,7 +211,7 @@ class SignInCubit<Extra> extends FormDataCubit<SignInState> {
|
||||
// TODO(wyatt): maybe emit new state (to not carry an old errorMessage)
|
||||
emit(state.copyWith(status: FormStatus.submissionInProgress));
|
||||
|
||||
final uid = await _authenticationRepository.signInWithGoogle();
|
||||
final uid = await authenticationRepository.signInWithGoogle();
|
||||
|
||||
emit(
|
||||
uid.fold(
|
||||
|
@ -26,23 +26,21 @@ part 'sign_up_state.dart';
|
||||
|
||||
class SignUpCubit<Extra> extends FormDataCubit<SignUpState> {
|
||||
SignUpCubit({
|
||||
required AuthenticationRepository<Extra> authenticationRepository,
|
||||
}) : _authenticationRepository = authenticationRepository,
|
||||
super(
|
||||
required this.authenticationRepository,
|
||||
}) : super(
|
||||
SignUpState(
|
||||
form: authenticationRepository.formRepository
|
||||
.accessForm(AuthFormName.signUpForm),
|
||||
),
|
||||
);
|
||||
final AuthenticationRepository<Extra> _authenticationRepository;
|
||||
FormRepository get _formRepository =>
|
||||
_authenticationRepository.formRepository;
|
||||
final AuthenticationRepository<Extra> authenticationRepository;
|
||||
FormRepository get formRepository => authenticationRepository.formRepository;
|
||||
|
||||
@override
|
||||
String get formName => AuthFormName.signUpForm;
|
||||
|
||||
void emailChanged(String value) {
|
||||
final emailValidatorType = _formRepository
|
||||
final emailValidatorType = formRepository
|
||||
.accessForm(formName)
|
||||
.validatorOf(AuthFormField.email)
|
||||
.runtimeType;
|
||||
@ -56,7 +54,7 @@ class SignUpCubit<Extra> extends FormDataCubit<SignUpState> {
|
||||
}
|
||||
|
||||
void passwordChanged(String value) {
|
||||
final passwordValidatorType = _formRepository
|
||||
final passwordValidatorType = formRepository
|
||||
.accessForm(formName)
|
||||
.validatorOf(AuthFormField.password)
|
||||
.runtimeType;
|
||||
@ -93,11 +91,11 @@ class SignUpCubit<Extra> extends FormDataCubit<SignUpState> {
|
||||
String key,
|
||||
FormInputValidator<Value, ValidationError> dirtyValue,
|
||||
) {
|
||||
final form = _formRepository.accessForm(formName).clone();
|
||||
final form = formRepository.accessForm(formName).clone();
|
||||
|
||||
try {
|
||||
form.updateValidator(key, dirtyValue);
|
||||
_formRepository.updateForm(form);
|
||||
formRepository.updateForm(form);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
@ -110,7 +108,7 @@ class SignUpCubit<Extra> extends FormDataCubit<SignUpState> {
|
||||
@override
|
||||
FutureOr<void> reset() {
|
||||
final form = state.form.reset();
|
||||
_formRepository.updateForm(form);
|
||||
formRepository.updateForm(form);
|
||||
emit(
|
||||
state.copyWith(form: form, status: form.validate()),
|
||||
);
|
||||
@ -124,7 +122,7 @@ class SignUpCubit<Extra> extends FormDataCubit<SignUpState> {
|
||||
|
||||
emit(state.copyWith(status: FormStatus.submissionInProgress));
|
||||
|
||||
final form = _formRepository.accessForm(formName);
|
||||
final form = formRepository.accessForm(formName);
|
||||
final email = form.valueOf<String?>(AuthFormField.email);
|
||||
final password = form.valueOf<String?>(AuthFormField.password);
|
||||
|
||||
@ -137,7 +135,7 @@ class SignUpCubit<Extra> extends FormDataCubit<SignUpState> {
|
||||
);
|
||||
}
|
||||
|
||||
final uid = await _authenticationRepository.signUp(
|
||||
final uid = await authenticationRepository.signUp(
|
||||
email: email!,
|
||||
password: password!,
|
||||
);
|
||||
@ -158,9 +156,9 @@ class SignUpCubit<Extra> extends FormDataCubit<SignUpState> {
|
||||
WyattForm form, {
|
||||
SetOperation operation = SetOperation.replace,
|
||||
}) {
|
||||
final WyattForm current = _formRepository.accessForm(formName).clone();
|
||||
final WyattForm current = formRepository.accessForm(formName).clone();
|
||||
final WyattForm newForm = operation.operation.call(current, form);
|
||||
_formRepository.updateForm(newForm);
|
||||
formRepository.updateForm(newForm);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
@ -174,7 +172,7 @@ class SignUpCubit<Extra> extends FormDataCubit<SignUpState> {
|
||||
FutureOr<void> validate() {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: _formRepository.accessForm(formName).validate(),
|
||||
status: formRepository.accessForm(formName).validate(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user