public async Task <ActionResult> UpdateEmail(UpdatedEmail updatedEmail) { // Get the customer from the database based on the customer id from the claims via the access token Customer customer = await userManager.FindByIdAsync(User.FindFirst(ClaimTypes.NameIdentifier).Value); // If the customer is found... if (customer != null) { if (!await userManager.CheckPasswordAsync(customer, updatedEmail.Password)) { return(Conflict("Your password and email do not match.")); } string previousEmail = customer.Email; // Update the new email in the database IdentityResult result = await userManager.ChangeEmailAsync(customer, updatedEmail.Email, updatedEmail.Token); // If the update was successful, return ok if (result.Succeeded) { // Send a confirmation email that the customer email has been changed if (customer.EmailPrefEmailChange == true) { emailService.AddToQueue(EmailType.EmailChange, "Email change confirmation", new Recipient { FirstName = customer.FirstName, LastName = customer.LastName, Email = updatedEmail.Email }, new EmailProperties { Host = GetHost(), Var1 = previousEmail, Var2 = updatedEmail.Email }); } return(Ok()); } else { return(Conflict("An error occured due to an invalid email address or invalid token.")); } } return(BadRequest()); }
public async Task <ActionResult> UpdateEmail(UpdatedEmail updatedEmail) { // Get the customer from the database based on the customer id from the claims via the access token Customer customer = await userManager.FindByIdAsync(User.FindFirst(ClaimTypes.NameIdentifier).Value); // If the customer is found... if (customer != null) { // Update the new email in the database IdentityResult result = await userManager.SetEmailAsync(customer, updatedEmail.Email); // If the update was successful, return the customer data with the new email if (result.Succeeded) { return(Ok(new { updatedEmail.Email })); } else { // The update was not successful. Return with errors foreach (IdentityError error in result.Errors) { if (error.Code == "DuplicateEmail") { error.Description = "The email address, \"" + updatedEmail.Email.ToLower() + "\", already exists with another Niche Shack account. Please use another email address."; } ModelState.AddModelError(error.Code, error.Description); } return(Conflict(ModelState)); } } return(BadRequest()); }