Beispiel #1
0
 private ObjectResult GetErrorResult(SignUpError errorType)
 {
     return(errorType switch
     {
         SignUpError.InvalidUsername => BadRequest(new ErrorResponse("Invalid username")),
         SignUpError.DuplicateUsername => BadRequest(new ErrorResponse("Duplicate username")),
         SignUpError.InvalidPassword => BadRequest(new ErrorResponse("Invalid password")),
         SignUpError.InvalidEmail => BadRequest(new ErrorResponse("Invalid email")),
         SignUpError.DuplicateEmail => BadRequest(new ErrorResponse("Duplicate email")),
         SignUpError.PasswordAndConfirmationNotSame => BadRequest(new ErrorResponse("Password and it's confirmation are different")),
         _ => throw new ArgumentOutOfRangeException(nameof(errorType), errorType, null)
     });
Beispiel #2
0
        private async Task <SignUpError> ValidateSignUpInfo(User userInfo)
        {
            var updateError = new SignUpError();

            if (string.IsNullOrWhiteSpace(userInfo.FirstName))
            {
                updateError.FirstName = new List <string> {
                    "Please enter a first name."
                };
                updateError.HasError = true;
            }

            if (string.IsNullOrWhiteSpace(userInfo.LastName))
            {
                updateError.LastName = new List <string> {
                    "Please enter a last name."
                };
                updateError.HasError = true;
            }

            if (string.IsNullOrWhiteSpace(userInfo.Email) ||
                (new EmailAddressAttribute().IsValid(userInfo.Email) == false))
            {
                updateError.Email = new List <string> {
                    "Please enter a valid eMail."
                };
                updateError.HasError = true;
            }
            else if (!string.IsNullOrWhiteSpace(userInfo.Email) &&
                     await _database.User.AnyAsync(u => u.Email == userInfo.Email.ToLower()))
            {
                updateError.Email = new List <string> {
                    "Please enter a valid eMail. This eMail is not unique."
                };
                updateError.HasError = true;
            }

            if (string.IsNullOrWhiteSpace(userInfo.ReadPassword) ||
                (userInfo.ReadPassword.Length < _appSettings.MinPasswordLength))
            {
                updateError.Password = new List <string> {
                    "Password is too short."
                };
                updateError.HasError = true;
            }

            if (!userInfo.Birthday.HasValue)
            {
                updateError.Birthday = new List <string> {
                    "Please enter a birthday."
                };
                updateError.HasError = true;
            }

            UserConstants.Genders gender;
            if (string.IsNullOrWhiteSpace(userInfo.Gender) || !Enum.TryParse(userInfo.Gender, true, out gender))
            {
                updateError.Gender = new List <string>
                {
                    $"Please enter a valid gender. {string.Join(",", Enum.GetNames(typeof(UserConstants.Genders)))}."
                };
                updateError.HasError = true;
            }

            return(updateError);
        }