mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-24 20:17:55 +00:00
Implemented sign up and otp screens
This commit is contained in:
@@ -19,8 +19,16 @@ class AuthCubit extends Cubit<AuthState> {
|
||||
final TextEditingController emailController = TextEditingController();
|
||||
final TextEditingController passwordController = TextEditingController();
|
||||
|
||||
final TextEditingController emailSignUpController = TextEditingController();
|
||||
final TextEditingController passwordSignUpController = TextEditingController();
|
||||
// final TextEditingController emailSignUpController = TextEditingController();
|
||||
// final TextEditingController fullNameController = TextEditingController();
|
||||
// final TextEditingController passwordSignUpController = TextEditingController();
|
||||
// final TextEditingController reEnterPasswordSignUpController = TextEditingController();
|
||||
|
||||
String fullName = '';
|
||||
String email = '';
|
||||
String signUpPassword = '';
|
||||
String maskedEmail = '';
|
||||
String otpCode = '';
|
||||
final loginFormKey = GlobalKey<FormState>();
|
||||
final signUpFormKey = GlobalKey<FormState>();
|
||||
bool isPasswordVisible = false;
|
||||
@@ -46,32 +54,69 @@ class AuthCubit extends Cubit<AuthState> {
|
||||
/////////////////////////////////////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 {
|
||||
if (value.isEmpty) {
|
||||
return 'Please enter your password';
|
||||
}
|
||||
if (value.isNotEmpty) {
|
||||
if (!RegExp(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$')
|
||||
.hasMatch(value)) {
|
||||
return 'Password must contain at least one uppercase letter, one lowercase letter, one number and one special character';
|
||||
}
|
||||
}
|
||||
}
|
||||
if (signUpFormKey.currentState != null) {
|
||||
signUpFormKey.currentState!.save();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
String? fullNameValidator(String? value) {
|
||||
if (value == null) return 'Full name is required';
|
||||
|
||||
final withoutExtraSpaces = value.replaceAll(RegExp(r"\s+"), ' ').trim();
|
||||
|
||||
if (withoutExtraSpaces.length < 2 || withoutExtraSpaces.length > 30) {
|
||||
return 'Full name must be between 2 and 30 characters long';
|
||||
}
|
||||
|
||||
// Test if it contains anything but alphanumeric spaces and single quote
|
||||
|
||||
if (RegExp(r"/[^ a-zA-Z0-9-\']/").hasMatch(withoutExtraSpaces)) {
|
||||
return 'Only alphanumeric characters, space, dash and single quote are allowed';
|
||||
}
|
||||
|
||||
final parts = withoutExtraSpaces.split(' ');
|
||||
|
||||
if (parts.length < 2) return 'Full name must contain first and last names';
|
||||
|
||||
if (parts.length > 3) return 'Full name can at most contain 3 parts';
|
||||
|
||||
if (parts.any((part) => part.length < 2 || part.length > 30)) {
|
||||
return 'Full name parts must be between 2 and 30 characters long';
|
||||
}
|
||||
if (signUpFormKey.currentState != null) {
|
||||
signUpFormKey.currentState!.save();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
String reEnterPasswordCheck(String? value) {
|
||||
passwordValidator(value);
|
||||
if (signUpPassword == value) {
|
||||
if (signUpFormKey.currentState != null) {
|
||||
signUpFormKey.currentState!.save();
|
||||
}
|
||||
return '';
|
||||
} else {
|
||||
return 'Passwords do not match';
|
||||
}
|
||||
}
|
||||
|
||||
String? emailAddressValidator(String? value) {
|
||||
if (value != null && value.isNotEmpty && value != "") {
|
||||
if (checkValidityOfEmail(value)) {
|
||||
if (loginFormKey.currentState != null) {
|
||||
loginFormKey.currentState!.save();
|
||||
if (signUpFormKey.currentState != null) {
|
||||
signUpFormKey.currentState!.save();
|
||||
}
|
||||
return null;
|
||||
} else {
|
||||
@@ -92,6 +137,23 @@ class AuthCubit extends Cubit<AuthState> {
|
||||
}
|
||||
}
|
||||
|
||||
// Function to mask the email
|
||||
String maskEmail(String email) {
|
||||
final emailParts = email.split('@');
|
||||
if (emailParts.length != 2) return email;
|
||||
|
||||
final localPart = emailParts[0];
|
||||
final domainPart = emailParts[1];
|
||||
|
||||
if (localPart.length < 3) return email;
|
||||
|
||||
final start = localPart.substring(0, 2);
|
||||
final end = localPart.substring(localPart.length - 1);
|
||||
|
||||
final maskedLocalPart = '$start******$end';
|
||||
return '$maskedLocalPart@$domainPart';
|
||||
}
|
||||
|
||||
/////////////////////////////////////API CALLS/////////////////////////////////////
|
||||
login() async {
|
||||
emit(AuthLoginLoading());
|
||||
@@ -128,21 +190,23 @@ class AuthCubit extends Cubit<AuthState> {
|
||||
emit(AuthLoginLoading());
|
||||
final response;
|
||||
try {
|
||||
List<String> userFullName = fullName.split(' ');
|
||||
response = await AuthenticationAPI.signUp(
|
||||
model: SignUpModel(
|
||||
email: emailController.text.toLowerCase(),
|
||||
password: passwordController.text,
|
||||
firstName: '',
|
||||
lastName: ''),
|
||||
email: email.toLowerCase(),
|
||||
password: signUpPassword,
|
||||
firstName: userFullName[0],
|
||||
lastName: userFullName[1]),
|
||||
);
|
||||
} catch (failure) {
|
||||
emit(AuthLoginError(message: failure.toString()));
|
||||
return;
|
||||
}
|
||||
if (response['statusCode'] == 201) {
|
||||
emailController.clear();
|
||||
passwordController.clear();
|
||||
emit(AuthLoginSuccess());
|
||||
if (response) {
|
||||
maskedEmail = maskEmail(email);
|
||||
final response = await AuthenticationAPI.sendOtp(body: {'email': email, 'type': 'PASSWORD'});
|
||||
otpCode = response['otp'];
|
||||
emit(AuthSignUpSuccess());
|
||||
} else {
|
||||
emit(AuthLoginError(message: 'Something went wrong'));
|
||||
}
|
||||
|
||||
@@ -20,6 +20,10 @@ class AuthLoginLoading extends AuthLoading {}
|
||||
|
||||
class AuthLoginSuccess extends AuthSuccess {}
|
||||
|
||||
class AuthOtpSuccess extends AuthSuccess {}
|
||||
|
||||
class AuthSignUpSuccess extends AuthSuccess {}
|
||||
|
||||
class AuthLoginError extends AuthError {
|
||||
AuthLoginError({required super.message, super.code});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user