diff --git a/lib/features/auth/bloc/auth_cubit.dart b/lib/features/auth/bloc/auth_cubit.dart index 9e46276..0c2fcdf 100644 --- a/lib/features/auth/bloc/auth_cubit.dart +++ b/lib/features/auth/bloc/auth_cubit.dart @@ -20,8 +20,9 @@ class AuthCubit extends Cubit { static AuthCubit get(context) => BlocProvider.of(context); - TextEditingController emailController = TextEditingController(); - TextEditingController passwordController = TextEditingController(); + final TextEditingController emailController = TextEditingController(); + final TextEditingController passwordController = TextEditingController(); + final loginFormKey = GlobalKey(); bool isPasswordVisible = false; static GlobalKey formKey = GlobalKey(); @@ -41,6 +42,56 @@ class AuthCubit extends Cubit { static UserModel? user; static Token token = Token.emptyConstructor(); + +/////////////////////////////////////VALIDATORS///////////////////////////////////// + String? passwordValidator(String? value) { + if (value != null) { + if (value.isNotEmpty) { + if (value.length >= 6) { + return null; + //TODO uncomment this code when the password validation is needed + // if (RegExp( + // r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$') + // .hasMatch(value)) { + // return null; + // } else { + // return 'Password must contain at least one uppercase letter, one lowercase letter, one number and one special character'; + // } + } else { + return 'Password must be at least 6 characters'; + } + } else { + return 'Please enter your password'; + } + } + return null; + } + + String? emailAddressValidator(String? value) { + if (value != null && value.isNotEmpty && value != "") { + if (checkValidityOfEmail(value)) { + if (loginFormKey.currentState != null) { + loginFormKey.currentState!.save(); + } + return null; + } else { + return 'Please enter a valid email'; + } + } else { + return 'Email address is required'; + } + } + + bool checkValidityOfEmail(String? email) { + if (email != null) { + return RegExp( + r"^[a-zA-Z0-9]+([.!#$%&'*+/=?^_`{|}~-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([.-]?[a-zA-Z0-9]+)*\.[a-zA-Z0-9]{2,}$") + .hasMatch(email); + } else { + return false; + } + } + /////////////////////////////////////API CALLS///////////////////////////////////// login() async { emit(AuthLoginLoading()); diff --git a/lib/features/auth/view/widgets/login/login_form.dart b/lib/features/auth/view/widgets/login/login_form.dart index 4fe33ee..5296e23 100644 --- a/lib/features/auth/view/widgets/login/login_form.dart +++ b/lib/features/auth/view/widgets/login/login_form.dart @@ -13,9 +13,10 @@ class LoginForm extends StatelessWidget { @override Widget build(BuildContext context) { - var formKey = GlobalKey(); + var pressed = false; return BlocBuilder( builder: (context, state) { + final formKey = AuthCubit.get(context).loginFormKey; return Form( key: formKey, autovalidateMode: AutovalidateMode.onUserInteraction, @@ -28,30 +29,23 @@ class LoginForm extends StatelessWidget { fontColor: Colors.white, ), TextFormField( + autovalidateMode: AutovalidateMode.disabled, + textInputAction: TextInputAction.done, + keyboardType: TextInputType.text, + scrollPadding: EdgeInsets.zero, + autocorrect: false, + autofillHints: const [AutofillHints.email], controller: AuthCubit.get(context).emailController, validator: (value) { - if (state is! AuthTokenError) { - if (value != null) { - if (value.isNotEmpty) { - if (RegExp( - r'^[a-zA-Z0-9._-]+@{1}[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$') - .hasMatch(value)) { - return null; - } else { - return 'Please enter a valid email'; - } - } else { - return 'Please enter your email'; - } - } else { - return 'Please enter your email'; - } + if (state is AuthTokenError && !pressed) { + return null; } - return null; + return AuthCubit.get(context).emailAddressValidator(value); }, onTapOutside: (event) { FocusScope.of(context).unfocus(); }, + onChanged: (value) {}, decoration: defaultInputDecoration(context, hint: "Example@email.com"), ), @@ -61,30 +55,18 @@ class LoginForm extends StatelessWidget { fontColor: Colors.white, ), TextFormField( + autovalidateMode: AutovalidateMode.disabled, + textInputAction: TextInputAction.done, + keyboardType: TextInputType.text, + scrollPadding: EdgeInsets.zero, + autocorrect: false, + autofillHints: const [AutofillHints.password], controller: AuthCubit.get(context).passwordController, validator: (value) { - if (state is! AuthTokenError) { - if (value != null) { - if (value.isNotEmpty) { - return null; - //TODO: uncomment this when the backend is ready - // if (value.length > 8) { - // if (RegExp( - // r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$') - // .hasMatch(value)) { - // return null; - // } else { - // return 'Password must contain at least one uppercase letter, one lowercase letter, one number and one special character'; - // } - // } else { - // return 'Password must be at least 8 characters'; - // } - } else { - return 'Please enter your password'; - } - } + if (state is AuthTokenError && !pressed) { + return null; } - return null; + return AuthCubit.get(context).passwordValidator(value); }, onTapOutside: (event) { FocusScope.of(context).unfocus(); @@ -116,14 +98,11 @@ class LoginForm extends StatelessWidget { 'Login', ), onPressed: () { - bool isValid = formKey.currentState!.validate(); - if (isValid) { + pressed = true; + if (formKey.currentState!.validate()) { if ((state is! AuthLoading)) { - print('login'); AuthCubit.get(context).login(); FocusScope.of(context).unfocus(); - } else { - formKey.currentState!.save(); } } },