User profile editing and password

This commit is contained in:
2016-09-26 11:13:59 -04:00
parent 6789c1b3b5
commit 75b7c02979
54 changed files with 373 additions and 209 deletions
@@ -1,6 +1,8 @@
using System.Web.Mvc;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using AutoMapper;
using InventoryTraker.Web.Core;
using InventoryTraker.Web.Attributes;
using InventoryTraker.Web.Identity;
using InventoryTraker.Web.Models;
using Microsoft.AspNet.Identity;
@@ -20,19 +22,44 @@ namespace InventoryTraker.Web.Controllers
public ActionResult Index()
{
var user = _userManager.FindById(User.Identity.GetUserId());
var model = _mapper.Map<ProfileForm>(user);
return View(model);
return View();
}
public JsonResult Update(ProfileForm form)
public JsonResult Get()
{
var user = _userManager.FindById(User.Identity.GetUserId());
user.Email = form.EmailAddress;
user.UserName = form.FullName;
_userManager.Update(user);
var model = _mapper.Map<ProfileForm>(user);
return BetterJson(model);
}
return Json(true);
[ActionLog]
public async Task<JsonResult> Update(ProfileForm form)
{
if (!ModelState.IsValid)
return GetModelStateErrorListJson();
var user = _userManager.FindById(User.Identity.GetUserId());
user.Email = form.Email;
user.UserName = form.UserName;
if (!string.IsNullOrEmpty(form.NewPassword))
{
if (string.IsNullOrEmpty(form.CurrentPassword))
return GetErrorListJson("Current password required");
var result =
await _userManager.ChangePasswordAsync(
user.Id, form.CurrentPassword, form.NewPassword);
if (!result.Succeeded)
return GetErrorListJson(result.Errors.ToArray());
}
var identityResult = _userManager.Update(user);
if (!identityResult.Succeeded)
return GetErrorListJson(identityResult.Errors.ToArray());
return BetterJson(_mapper.Map<ProfileForm>(user));
}
}
}
@@ -74,8 +74,7 @@ namespace InventoryTraker.Web.Controllers
if (!string.IsNullOrEmpty(form.Password))
{
var resetToken = await _userManager.GeneratePasswordResetTokenAsync(user.Id);
var resetResult = await _userManager.ResetPasswordAsync(user.Id, resetToken, form.Password);
var resetResult = await _userManager.ChangePasswordAsync(user, form.Password);
if (!resetResult.Succeeded)
return GetErrorListJson(resetResult.Errors.ToArray());
}