private async void RegisterProcess(UserRegistration singleuser) { await manager.SaveTaskAsync(singleuser); //await manager.CurrentClient.GetTable<UserRegistration>().InsertAsync(singleuser); }
public RegistrationViewModel() { //if session is valid, redirect the user if (Application.Current.Properties.ContainsKey("email")) { Context.Navigation.PushModalAsync(new TabPage()); } manager = RegistrationManager.DefaultManager; register_clicked = new Command(async() => { //create db if not exists int count = 0; bool validEmail = await IsEmailValid(Email); //if any field is empty if (First == null || Last == null || Mobile == null || Email == null || Password == null || Confirm_password == null) { await Context.DisplayAlert("Error", "You can't leave any field empty.", "OK"); count++; } //if the email already exists else if (!validEmail) { await Context.DisplayAlert("Error", "The email already exists. Please use a different email.", "OK"); count++; } //if password doesn't match else if (Password != Confirm_password) { await Context.DisplayAlert("Error", "Password Doesn't match.", "OK"); count++; } //if password length is less than 6 else if (Password.Length < 6) { await Context.DisplayAlert("Error", "Password Length should be greater than 5.", "OK"); count++; } //if mobile number length is less than 10 and does not cosist of numerical numbers else if (!Regex.IsMatch(Mobile, @"^[0-9]{10}$")) { await Context.DisplayAlert("Error", "Mobile Number length must be 10 and consist of numerical numbers.", "OK"); count++; } //check if it is name else if (!Regex.IsMatch(First, @"^[\p{L} \.\-]+$") || !Regex.IsMatch(Last, @"^[\p{L} \.\-]+$")) { await Context.DisplayAlert("Error", "Your first name or last name is not a name", "OK"); count++; } //check if it is in email format, catch the exception when it is not formatted well if (count <= 0) { try { MailAddress m = new MailAddress(Email); } catch (FormatException) { await Context.DisplayAlert("Error", "Please check the format of your email.", "OK"); count++; } catch (System.ArgumentNullException) { await Context.DisplayAlert("Error", "You can't leave any field empty.", "OK"); count++; } } //if there is no problem, insert the user to db if (count <= 0) { UserRegistration registeruser = new UserRegistration(); registeruser.Email = Email; registeruser.FirstName = First; registeruser.LastName = Last; registeruser.ImageURL = ""; //encrypt password byte[] encryptpwd = System.Text.Encoding.ASCII.GetBytes(Password); encryptpwd = new System.Security.Cryptography.SHA256Managed().ComputeHash(encryptpwd); String hashedpwd = System.Text.Encoding.ASCII.GetString(encryptpwd); registeruser.Pwd = hashedpwd; registeruser.PhoneNumber = Mobile; try { RegisterProcess(registeruser); } catch (Exception ex) { Debug.WriteLine(ex.Message.ToString()); } await Context.DisplayAlert("Successful", "You have successfully registered.", "OK"); await Context.Navigation.PushModalAsync(new LogIn()); } });//register_clicked event triggered signin_clicked = new Command(() => { Context.Navigation.PushModalAsync(new LogIn()); }); }
public LogInViewModel() { //if session is valid, redirect the user if (Application.Current.Properties.ContainsKey("email")) { Context.Navigation.PushModalAsync(new TabPage()); } manager = RegistrationManager.DefaultManager; register_clicked = new Command(async() => { await Context.Navigation.PushModalAsync(new Registration()); }); login_Clicked = new Command(async() => { int count = 0; UserRegistration getsingleuser = null; if (Email == null || Password == null) { await Context.DisplayAlert("Error", "You can't leave any field empty.", "OK"); count++; } else { try { var findpwd = await manager.GetPassword(Email); var list = new List <UserRegistration>(findpwd); getsingleuser = list.ToArray()[0]; } catch (IndexOutOfRangeException) { await Context.DisplayAlert("Error", "The email doesn't exist", "OK"); count++; } } if (count <= 0 && !Application.Current.Properties.ContainsKey("email")) { try { byte[] encryptpwd = System.Text.Encoding.ASCII.GetBytes(Password); encryptpwd = new System.Security.Cryptography.SHA256Managed().ComputeHash(encryptpwd); String hashedpwd = System.Text.Encoding.ASCII.GetString(encryptpwd); if (getsingleuser.Pwd == hashedpwd && count <= 0) { Application.Current.Properties["first"] = getsingleuser.FirstName; Application.Current.Properties["last"] = getsingleuser.LastName; Application.Current.Properties["phone"] = getsingleuser.PhoneNumber; Application.Current.Properties["email"] = getsingleuser.Email; Application.Current.Properties["password"] = getsingleuser.Pwd; //Application.Current.Properties.Clear(); await Context.DisplayAlert("Successful", "You now logged in.", "OK"); await Context.Navigation.PushModalAsync(mainPage); } else { await Context.DisplayAlert("Error", "You have entered a wrong password.", "OK"); count++; } } catch (NullReferenceException) { count++; } } }); }