public IActionResult AddProfile([FromBody] Profile profile) { try { if (!_profileRepository.UserHasProfile(profile.UserId)) { if (IdentityHelper.GetUserId(HttpContext) == profile.UserId) { _profileRepository.AddProfile(profile); return(Ok()); } else { // profile.UserId did not match the current UserId return(Unauthorized()); } } else { // User already has a profile return(NotFound()); } } catch { return(NotFound()); } }
public async Task<IActionResult> AddUser([FromBody]CredentialsViewModel userViewModel, bool isAdmin = false, string role ="User") { AppUser user = new AppUser{ UserName = userViewModel.UserName, Email = userViewModel.Email, FirstName = userViewModel.FirstName, LastName = userViewModel.LastName, PhoneNumber = userViewModel.PhoneNumber }; if(userViewModel.Role == "Admin" && !isAdmin) { return NotFound(); } if(userViewModel.Role == null) { userViewModel.Role = "User"; } var currentUser = await _userManager.FindByEmailAsync(user.Email); try { if(currentUser == null) { var createUser = await _userManager.CreateAsync(user, userViewModel.Password); if (createUser.Succeeded) { role = (isAdmin) ? "Admin" : userViewModel.Role; await _userManager.AddToRoleAsync(user,role); string uId = user.Id; Profile profile = new Profile { UserId = uId, FirstName = user.FirstName, LastName = user.LastName, Email = user.Email, Bio = "" }; _profileRepo.AddProfile(profile); return Ok(); } } return NotFound(); } catch { return NotFound(); } }
public ActionResult SubmitProfile(string firstName, string lastName, string email) { var result = _profileRepository.AddProfile(firstName, lastName, email); return(Json(new { success = true, reponseText = nameof(result) })); }