public async Task<Object> UpdateUser([FromBody]TOAppUser updatedUser) { string userId = User.Claims.First(c => c.Type == "UserID").Value; string role = User.Claims.First(c => c.Type == "Roles").Value; try { var user = await _userManager.FindByIdAsync(userId); user.Name = updatedUser.Name; user.Surname = updatedUser.Surname; user.City = updatedUser.City; user.PhoneNumber = updatedUser.PhoneNumber; await _userManager.UpdateAsync(user); return new { user.Name, user.Surname, user.Email, user.UserName, user.City, user.Company, user.PhoneNumber, role, }; } catch (Exception ex) { throw ex; } }
public void FromTO(TOAppUser toAppUser) { UserName = toAppUser.UserName; Email = toAppUser.Password; Name = toAppUser.Name; Surname = toAppUser.Surname; City = toAppUser.City; PhoneNumber = toAppUser.PhoneNumber; Company = toAppUser.Company; IsFirstLogIn = toAppUser.IsFirstLogIn; }
//POST : /api/AppUsers/Register public async Task <Object> PostApplicationUser(TOAppUser model) { var user = await _userManager.FindByEmailAsync(model.Email); if (user != null) { return(Ok("Email is allready taken")); //return StatusCode(400, "Email is already taken"); } var applicationUser = new AppUser() { UserName = model.UserName, Email = model.Email, Name = model.Name, Surname = model.Surname, PhoneNumber = model.PhoneNumber, City = model.City, Company = model.Company, IsFirstLogIn = true }; try { var result = await _userManager.CreateAsync(applicationUser, model.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(applicationUser, model.Role); string token = await _userManager.GenerateEmailConfirmationTokenAsync(applicationUser); MailingService.SendEmailVerification(applicationUser, model.Role, token, model.Password); } return(Ok(result)); } catch (Exception ex) { throw ex; } }