/// <summary>
        /// "Create" - button clicked.
        /// Validates and creates new user.
        /// Redirect to Home page if successfull.
        /// </summary>
        public async void CreateButtonClicked()
        {
            using (UserDialogs.Instance.Loading("Creating user..."))
            {
                // Run validation on input fields.
                string[] emailErrors = await DefaultValidator.ValidEmailCreateNewAsync(this.Email);

                string[] passwordErrors = DefaultValidator.ValidPassword(this.Password);
                string[] usernameErrors = DefaultValidator.ValidUsername(this.Username);

                // Check for errors
                if (emailErrors.Length == 0 && passwordErrors.Length == 0 && usernameErrors.Length == 0)
                {
                    // No errors
                    // Create user in database
                    try
                    {
                        await this._userService.CreateNewUserAsync(Email, Password, Username);


                        // Redirect to login page
                        await Application.Current.MainPage.Navigation.PushModalAsync(new AboutPage(), true);
                    }
                    catch (Exception e)
                    {
                        // An unexpected error occured, so user could not be created.
                        // Show generic error message to user.
                        ShowAlert("An error occured. User could not be created.");
                    }
                }
                else
                {
                    // Errors in user input.
                    // Display errors.
                    string errorMessage = "Invalid user data:" + System.Environment.NewLine;

                    foreach (var error in emailErrors)
                    {
                        errorMessage += System.Environment.NewLine + error;
                    }
                    foreach (var error in passwordErrors)
                    {
                        errorMessage += System.Environment.NewLine + error;
                    }
                    foreach (var error in usernameErrors)
                    {
                        errorMessage += System.Environment.NewLine + error;
                    }

                    ShowAlert(errorMessage);

                    // Color fields with errors.
                }
            }
        }
Example #2
0
        public void ValidPassword_ValidValues_ShouldReturnEmptyErrorList(string validateString)
        {
            // Arrange
            string testString = validateString;

            // Act
            var errorList = DefaultValidator.ValidPassword(validateString);

            // Assert
            Assert.IsEmpty(errorList);
        }
        /// <summary>
        /// Login button click event.
        /// </summary>
        public async void LoginButtonClicked()
        {
            using (UserDialogs.Instance.Loading("Validating..."))
            {
                string[] emailErrors    = DefaultValidator.ValidEmailLogin(this.Email);
                string[] passwordErrors = DefaultValidator.ValidPassword(this.Password);

                if (emailErrors.Length == 0 && passwordErrors.Length == 0)
                {
                    using (UserDialogs.Instance.Loading("Logging in..."))
                    {
                        try
                        {
                            // Login values are valid
                            // Proceed to login validation
                            bool loginSucceed = await this._userService.LoginUserAsync(this.Email, this.Password);

                            if (loginSucceed)
                            {
                                // User login was successfull
                                // Save user data to local DB, for login after app shutdown.
                                await App.LocalUserDatabase.SaveLocalUserDataAsync(new Models.UserLocal(Email, Password));

                                // Redirect to main page
                                this.RedirectToMainPage();
                            }
                            else
                            {
                                // User login was not successfull
                                // Empty input fields and display error

                                this.Email    = "";
                                this.Password = "";

                                ShowAlert("Wrong email or password.");
                            }
                        }
                        catch
                        {
                            ShowAlert("An unkown error occured. Please try again.");
                        }
                    }
                }
                else
                {
                    // Login values are not valid
                    // Display error message
                    string errorMessage = "An error occured during login" + System.Environment.NewLine;

                    foreach (var error in emailErrors)
                    {
                        errorMessage += System.Environment.NewLine + error;
                    }
                    foreach (var error in passwordErrors)
                    {
                        errorMessage += System.Environment.NewLine + error;
                    }

                    ShowAlert(errorMessage);
                }
            }
        }