fix(authentication): allow email/password validators customization (closes #57)
This commit was merged in pull request #60.
This commit is contained in:
+18
-4
@@ -57,6 +57,8 @@ class AuthenticationRepositoryImpl<T extends Object>
|
||||
FormRepository? formRepository,
|
||||
// ignore: strict_raw_type
|
||||
List<FormInput>? extraSignUpInputs,
|
||||
FormInputValidator<String?, ValidationError>? customEmailValidator,
|
||||
FormInputValidator<String?, ValidationError>? customPasswordValidator,
|
||||
OnSignUpSuccess<T>? onSignUpSuccess,
|
||||
OnAuthChange<T>? onAuthChange,
|
||||
}) : _authenticationLocalDataSource = authenticationCacheDataSource,
|
||||
@@ -71,8 +73,14 @@ class AuthenticationRepositoryImpl<T extends Object>
|
||||
..registerForm(
|
||||
WyattFormImpl(
|
||||
[
|
||||
FormInput(AuthFormField.email, const Email.pure()),
|
||||
FormInput(AuthFormField.password, const Password.pure())
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
customEmailValidator ?? const Email.pure(),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
customPasswordValidator ?? const Password.pure(),
|
||||
)
|
||||
],
|
||||
name: AuthFormName.signInForm,
|
||||
),
|
||||
@@ -80,8 +88,14 @@ class AuthenticationRepositoryImpl<T extends Object>
|
||||
..registerForm(
|
||||
WyattFormImpl(
|
||||
[
|
||||
FormInput(AuthFormField.email, const Email.pure()),
|
||||
FormInput(AuthFormField.password, const Password.pure()),
|
||||
FormInput(
|
||||
AuthFormField.email,
|
||||
customEmailValidator ?? const Email.pure(),
|
||||
),
|
||||
FormInput(
|
||||
AuthFormField.password,
|
||||
customPasswordValidator ?? const Password.pure(),
|
||||
),
|
||||
...extraSignUpInputs ?? []
|
||||
],
|
||||
name: AuthFormName.signUpForm,
|
||||
|
||||
+38
-1
@@ -41,15 +41,52 @@ class SignInCubit<Extra> extends FormDataCubit<SignInState> {
|
||||
String get formName => AuthFormName.signInForm;
|
||||
|
||||
void emailChanged(String value) {
|
||||
final emailValidatorType = _formRepository
|
||||
.accessForm(formName)
|
||||
.validatorOf(AuthFormField.email)
|
||||
.runtimeType;
|
||||
assert(
|
||||
emailValidatorType == Email,
|
||||
'Use emailCustomChanged(...) with validator $emailValidatorType',
|
||||
);
|
||||
|
||||
final Email email = Email.dirty(value);
|
||||
dataChanged(AuthFormField.email, email);
|
||||
}
|
||||
|
||||
void passwordChanged(String value) {
|
||||
final passwordValidatorType = _formRepository
|
||||
.accessForm(formName)
|
||||
.validatorOf(AuthFormField.password)
|
||||
.runtimeType;
|
||||
assert(
|
||||
passwordValidatorType == Password,
|
||||
'Use passwordCustomChanged(...) with validator $passwordValidatorType',
|
||||
);
|
||||
final Password password = Password.dirty(value);
|
||||
dataChanged(AuthFormField.password, password);
|
||||
}
|
||||
|
||||
/// Same as [emailChanged] but with a custom [Validator].
|
||||
///
|
||||
/// Sort of short hand for [dataChanged].
|
||||
void emailCustomChanged<
|
||||
Validator extends FormInputValidator<String?, ValidationError>>(
|
||||
Validator validator,
|
||||
) {
|
||||
dataChanged(AuthFormField.email, validator);
|
||||
}
|
||||
|
||||
/// Same as [passwordChanged] but with a custom [Validator].
|
||||
///
|
||||
/// Sort of short hand for [dataChanged].
|
||||
void passwordCustomChanged<
|
||||
Validator extends FormInputValidator<String?, ValidationError>>(
|
||||
Validator validator,
|
||||
) {
|
||||
dataChanged(AuthFormField.password, validator);
|
||||
}
|
||||
|
||||
@override
|
||||
FutureOr<void> dataChanged<Value>(
|
||||
String key,
|
||||
@@ -114,7 +151,7 @@ class SignInCubit<Extra> extends FormDataCubit<SignInState> {
|
||||
if (state.status.isSubmissionInProgress) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (!state.status.isValidated) {
|
||||
return;
|
||||
}
|
||||
|
||||
+4
-2
@@ -17,8 +17,10 @@
|
||||
part of 'sign_in_cubit.dart';
|
||||
|
||||
class SignInState extends FormDataState {
|
||||
Email get email => form.validatorOf(AuthFormField.email);
|
||||
Password get password => form.validatorOf(AuthFormField.password);
|
||||
FormInputValidator<String?, ValidationError> get email =>
|
||||
form.validatorOf(AuthFormField.email);
|
||||
FormInputValidator<String?, ValidationError> get password =>
|
||||
form.validatorOf(AuthFormField.password);
|
||||
|
||||
const SignInState({
|
||||
required super.form,
|
||||
|
||||
@@ -43,15 +43,52 @@ class SignUpCubit<Extra> extends FormDataCubit<SignUpState> {
|
||||
String get formName => AuthFormName.signUpForm;
|
||||
|
||||
void emailChanged(String value) {
|
||||
final emailValidatorType = _formRepository
|
||||
.accessForm(formName)
|
||||
.validatorOf(AuthFormField.email)
|
||||
.runtimeType;
|
||||
assert(
|
||||
emailValidatorType == Email,
|
||||
'Use emailCustomChanged(...) with validator $emailValidatorType',
|
||||
);
|
||||
|
||||
final Email email = Email.dirty(value);
|
||||
dataChanged(AuthFormField.email, email);
|
||||
}
|
||||
|
||||
void passwordChanged(String value) {
|
||||
final passwordValidatorType = _formRepository
|
||||
.accessForm(formName)
|
||||
.validatorOf(AuthFormField.password)
|
||||
.runtimeType;
|
||||
assert(
|
||||
passwordValidatorType == Password,
|
||||
'Use passwordCustomChanged(...) with validator $passwordValidatorType',
|
||||
);
|
||||
final Password password = Password.dirty(value);
|
||||
dataChanged(AuthFormField.password, password);
|
||||
}
|
||||
|
||||
/// Same as [emailChanged] but with a custom [Validator].
|
||||
///
|
||||
/// Sort of short hand for [dataChanged].
|
||||
void emailCustomChanged<
|
||||
Validator extends FormInputValidator<String?, ValidationError>>(
|
||||
Validator validator,
|
||||
) {
|
||||
dataChanged(AuthFormField.email, validator);
|
||||
}
|
||||
|
||||
/// Same as [passwordChanged] but with a custom [Validator].
|
||||
///
|
||||
/// Sort of short hand for [dataChanged].
|
||||
void passwordCustomChanged<
|
||||
Validator extends FormInputValidator<String?, ValidationError>>(
|
||||
Validator validator,
|
||||
) {
|
||||
dataChanged(AuthFormField.password, validator);
|
||||
}
|
||||
|
||||
@override
|
||||
FutureOr<void> dataChanged<Value>(
|
||||
String key,
|
||||
|
||||
+4
-2
@@ -17,8 +17,10 @@
|
||||
part of 'sign_up_cubit.dart';
|
||||
|
||||
class SignUpState extends FormDataState {
|
||||
Email get email => form.validatorOf(AuthFormField.email);
|
||||
Password get password => form.validatorOf(AuthFormField.password);
|
||||
FormInputValidator<String?, ValidationError> get email =>
|
||||
form.validatorOf(AuthFormField.email);
|
||||
FormInputValidator<String?, ValidationError> get password =>
|
||||
form.validatorOf(AuthFormField.password);
|
||||
|
||||
const SignUpState({
|
||||
required super.form,
|
||||
|
||||
Reference in New Issue
Block a user