public async Task UpdateUserProfile(UpdateUserProfileApiModel update) { _log.LogWarning("UpdateUserProfile_SignalR - " + Context.User.Identity.Name); var user = await _userManager.GetUserAsync(Context.User); if (user == null) { return; } if (!string.IsNullOrWhiteSpace(update.NewEmail)) { var userFound = await _userManager.FindByEmailAsync(update.NewEmail); if (userFound == null || user != userFound) { return; } } // Send to everyone that this user is online await Clients.Others.SendAsync("UpdateFriend", update); }
public async Task <string> UpdateUserProfieAsync([FromBody] UpdateUserProfileApiModel updateProfile) { _log.LogWarning("UpdateUserProfileAsync - " + HttpContext.User.Identity.Name); // The message when we fail to login var errorMessage = "Nu ati introdus datele corect!"; // The error response for a failed login var errorResponse = new ApiResponse <string> { // Set error message ErrorMessage = errorMessage }; // Get user claims var user = await _userManager.GetUserAsync(HttpContext.User); // Make sure the user exists if (user == null) { // Return error errorResponse.ErrorMessage = "Utilizator nu a fost gasit!"; return(JsonConvert.SerializeObject(errorResponse)); } // Make sure users match via email if (!user.Email.Equals(updateProfile.CurrentEmail)) { errorResponse.ErrorMessage = "Utilizator nu a fost gasit"; return(JsonConvert.SerializeObject(errorResponse)); } // Update new user data // Check if your email needs updating if (!string.IsNullOrWhiteSpace(updateProfile.NewEmail)) { // Get user with this new email var userFound = await _userManager.FindByEmailAsync(updateProfile.NewEmail); // Check if there is a user with such an email if (userFound != null) { // Return error errorResponse.ErrorMessage = "Astfel de email exista deja!"; return(JsonConvert.SerializeObject(errorResponse)); } // Updates the email user.Email = updateProfile.NewEmail; // Updates the user name user.UserName = updateProfile.NewEmail; } // Check if your first name needs updating if (!string.IsNullOrWhiteSpace(updateProfile.FirstName)) { // Updates the first name user.FirstName = updateProfile.FirstName; } // Check if your last name needs updating if (!string.IsNullOrWhiteSpace(updateProfile.LastName)) { // Updates the last name user.LastName = updateProfile.LastName; } // Update the new user data in the database var result = await _userManager.UpdateAsync(user); // Find the user with the updated data var userIdentity = await _userManager.FindByEmailAsync(user.Email); // Check if the data was not successfully updated or the user was not found if (!result.Succeeded || userIdentity == null) { errorResponse.ErrorMessage = "Nu sa putut actualiza datele."; return(JsonConvert.SerializeObject(errorResponse)); } // If the data has been updated and the user has been found returns a token return(JsonConvert.SerializeObject(new ApiResponse <string> { // Send the user's token Response = userIdentity.GenerateJwtToken() })); }