Example #1
0
        public async Task <IActionResult> RegisterUser([FromBody] RegisterRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new ResponseModel <AppUser> {
                    IsSuccess = false, Message = "Invalid form data sent", Data = null
                }));
            }

            var newAddress = await _generalService.AddNewAddress(request.Address);

            var hashedObj = _passwordHash.HashPassword(request.Password);
            var newUser   = new AppUser();

            newUser.FirstName      = request.FirstName;
            newUser.LastName       = request.LastName;
            newUser.Email          = request.Email;
            newUser.HashedPassword = hashedObj.Password;
            newUser.Salt           = hashedObj.Salt;
            newUser.AddressId      = newAddress.AddressId;


            var createdUser = await _userService.CreateUser(newUser);

            createdUser.AppUserRoles.Add(new AppUserRoles {
                AppUserId = createdUser.AppUserId, AppRoleId = 5
            });
            var dbUser = await _userService.UpdateUser(createdUser);

            return(Ok(new ResponseModel <AppUser> {
                IsSuccess = true, Data = dbUser, Message = $"Registered new account for {newUser.FirstName}"
            }));
        }