Merge branch 'dev' into doorlock_interface

This commit is contained in:
Mohammad Salameh
2024-04-17 14:01:54 +03:00
9 changed files with 143 additions and 154 deletions

View File

@ -20,8 +20,9 @@ class AuthCubit extends Cubit<AuthState> {
static AuthCubit get(context) => BlocProvider.of(context);
TextEditingController emailController = TextEditingController();
TextEditingController passwordController = TextEditingController();
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
final loginFormKey = GlobalKey<FormState>();
bool isPasswordVisible = false;
static GlobalKey<FormState> formKey = GlobalKey<FormState>();
@ -41,6 +42,56 @@ class AuthCubit extends Cubit<AuthState> {
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());

View File

@ -13,9 +13,10 @@ class LoginForm extends StatelessWidget {
@override
Widget build(BuildContext context) {
var formKey = GlobalKey<FormState>();
var pressed = false;
return BlocBuilder<AuthCubit, AuthState>(
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();
}
}
},