public async Task <IActionResult> Register([FromForm] RegisterNewUserAccountView registerNewUserView) { if (ModelState.IsValid) { try { var url = new Uri($"{Request.Scheme}://{Request.Host}"); registerNewUserView.CurrentUrl = url; if (registerNewUserView.Type.ToString() == AppConstants.CLIENT_ROLE) { await _accountService.RegisterNewUser(registerNewUserView); } else if (registerNewUserView.Type.ToString() == AppConstants.DRIVER_ROLE) { await _accountService.RegisterNewDriver(registerNewUserView); } else { throw new AccountException("Cant register this type"); } return(RedirectToAction("LogIn", "Account")); } catch (AccountException ex) { ModelState.AddModelError(string.Empty, ex.Message); return(View("Register")); } } return(View("Register")); }
public async Task RegisterNewUser(RegisterNewUserAccountView viewModel) { var user = new AppUser(); user.Email = viewModel.Email; user.UserName = viewModel.Username; if (!viewModel.Password.Equals(viewModel.ConfirmPassword)) { throw new AccountException("Passwords are different"); } if (!(await _userManager.FindByEmailAsync(viewModel.Email) is null) || !(await _userManager.FindByNameAsync(viewModel.Username) is null)) { throw new AccountException("Account with such Email or UserID already exists"); } IdentityResult result = await _userManager.CreateAsync(user, viewModel.Password); if (!result.Succeeded) { string responseError = result.GetFirstError(); throw new AccountException(responseError); } AppUser userRegistered = await _userManager.FindByEmailAsync(viewModel.Email); if (viewModel.Type.ToString() == AppConstants.CLIENT_ROLE) { await _userManager.AddToRoleAsync(userRegistered, AppConstants.CLIENT_ROLE); } else if (viewModel.Type.ToString() == AppConstants.DRIVER_ROLE) { await _userManager.AddToRoleAsync(userRegistered, AppConstants.DRIVER_ROLE); } else { throw new AccountException("Cant register this type"); } //TO DO //await EmailConfirmation(userRegistered, viewModel.CurrentUrl); }