Skip to content

Commit

Permalink
feat: validate mail/password
Browse files Browse the repository at this point in the history
  • Loading branch information
VincePaulin committed Mar 28, 2024
1 parent 3e06911 commit 0715f05
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
4 changes: 3 additions & 1 deletion assets/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -2549,11 +2549,13 @@
"register_pleaseConfirmYourPassword": "Please confirm your password",
"register_notSamePassword": "Passwords don't match",
"register_requiredField": "This field is required",
"register_emailError": "Please enter a valid email",
"register_passwordErrorOne": "Password must be 3-16 characters long and contain only lowercase letters and numbers.",
"register_passwordErrorTwo": "Password cannot contain common keywords.",
"username_pageTitle": "Choose Username",
"username_yourPosition": "Your position in the list:",
"username_itsYourTurn": "It's your turn!",
"username_changeUsername": "Choose your Tawkie name",
"username_advertisement": "This username is reserved for you. If you change it, you risk losing it."
"username_advertisement": "This username is reserved for you. If you change it, you risk losing it.",
"username_success": "Successful name change for"
}
30 changes: 26 additions & 4 deletions lib/pages/register/register.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,27 @@ class RegisterController extends State<Register> {
void toggleShowPassword() =>
setState(() => showPassword = !loading && !showPassword);

bool _validateEmail(String email) {
// Define regex to validate email format
final RegExp emailRegex = RegExp(
r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$',
);

// Check if the email matches the regex
if (!emailRegex.hasMatch(email)) {
setState(() => emailError = L10n.of(context)?.register_emailError);
return false;
}

// Reset email error if valid
setState(() => emailError = null);
return true;
}

bool _validatePassword(String password) {
// Define regex to validate password format
final RegExp passwordRegex = RegExp(r'^[a-z0-9]{3,16}$');

// List of keywords to check
final List<String> keywords = ['password', '123456', 'qwerty'];
final RegExp passwordRegex =
RegExp(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{3,16}$');

// Check that the password matches the regex
if (!passwordRegex.hasMatch(password)) {
Expand All @@ -51,6 +66,7 @@ class RegisterController extends State<Register> {
}

// Check if the password contains one of the following keywords
final List<String> keywords = ['password', '123456', 'qwerty'];
for (final keyword in keywords) {
if (password.contains(keyword)) {
setState(
Expand All @@ -65,13 +81,19 @@ class RegisterController extends State<Register> {
}

Future<void> register() async {
setState(() => confirmPasswordError = null);

if (emailController.text.isEmpty) {
setState(() => emailError = L10n.of(context)!.register_requiredField);
return;
} else {
setState(() => emailError = null);
}

if (!_validateEmail(emailController.text)) {
return;
}

if (passwordController.text.isEmpty) {
setState(() => passwordError = L10n.of(context)!.register_requiredField);
return;
Expand Down

0 comments on commit 0715f05

Please sign in to comment.