-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add users #190
base: develop
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice PR!
{ | ||
using (var sha256 = SHA256.Create()) | ||
{ | ||
var hashedBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(password)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good job on password hashing!
if (user.Role == "User") | ||
{ | ||
await DisplayAlert("Notice", "Contact manager", "OK"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using else if's would greatly reduce the nesting, which would make it way more readable
if (txtName.Text != null) | ||
{ | ||
if (txtEmail.Text != null) | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, inverting the conditions would reduce nesting greatly.
would also help to refactor this into multiple methods
private async void LoginButton_Clicked(object sender, EventArgs e) | ||
{ | ||
string email = txtLoginEmail.Text; string password = txtLoginPassword.Text; | ||
var user = users.FirstOrDefault(u => u.Email == email && PasswordHasher.VerifyPassword(password, u.Password)); | ||
|
||
if (user != null) | ||
{ | ||
if (user.Role == "User") | ||
{ | ||
await DisplayAlert("Notice", "Contact manager", "OK"); | ||
} | ||
else | ||
{ | ||
if (user.Role != "Admin") | ||
{ | ||
userManagementSection.IsVisible = true; | ||
} | ||
else | ||
{ | ||
if (email == "[email protected]") | ||
{ | ||
userManagementSection.IsVisible = true; | ||
} | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
if (email == "1" && password == "1") | ||
{ | ||
userManagementSection.IsVisible = true; | ||
} | ||
else | ||
{ | ||
await DisplayAlert("Login Failed", "Invalid credentials", "OK"); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code contains multiple nested if statements, making it difficult to read and understand. It could be simplified significantly by using guard clauses. Also, the code's formatting is inconsistent (e.g., the first two variable declarations are on the same line), and the method checks for the role and email in a way that could be simplified.
private async void AddUserButton_Clicked(object sender, EventArgs e) | ||
{ | ||
if (txtName.Text != null) | ||
{ | ||
if (txtEmail.Text != null) | ||
{ | ||
if (txtPassword.Text != null) | ||
{ | ||
if (txtTeam.Text != null) | ||
{ | ||
if (IsValidEmail(txtEmail.Text)) | ||
{ | ||
var newUser = new User | ||
{ | ||
Name = txtName.Text, | ||
Email = txtEmail.Text, | ||
Password = PasswordHasher.HashPassword(txtPassword.Text), | ||
Team = txtTeam.Text | ||
}; | ||
if (pickerRole.SelectedItem != null) | ||
{ | ||
newUser.Role = (string)pickerRole.SelectedItem; | ||
if (pickerAccessLevel.SelectedItem != null) | ||
{ | ||
newUser.AccessLevel = (string)pickerAccessLevel.SelectedItem; | ||
await userService.Add(newUser); | ||
users.Add(newUser); | ||
ClearUserInputFields(); | ||
await DisplayAlert("Success", $"{newUser.Name} added!", "OK"); | ||
} | ||
else await DisplayAlert("Error", "Access Level not selected", "OK"); | ||
} | ||
else await DisplayAlert("Error", "Role not selected", "OK"); | ||
} | ||
else await DisplayAlert("Error", "Invalid Email", "OK"); | ||
} | ||
else await DisplayAlert("Error", "Team is empty", "OK"); | ||
} | ||
else await DisplayAlert("Error", "Password is empty", "OK"); | ||
} | ||
else await DisplayAlert("Error", "Email is empty", "OK"); | ||
} | ||
else await DisplayAlert("Error", "Name is empty", "OK"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the code has too many nested if statements, making it hard to follow, there are also unnecessary checks for null on text fields that are typically empty strings rather than null in UI contexts, and there's no need for multiple await DisplayAlert statements for each field check, they just lead to repetition and clutter. Try implementing this code using guard clauses
added for this week