public async Task<IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var user = new ApplicationUser() { FirstName = model.FirstName, LastName = model.LastName, UserName = model.Username, Email = model.Email, }; if (model.CreateHousehold) user.Household = Guid.NewGuid().ToString(); IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return GetErrorResult(result); } return Ok(); }
public async Task<IHttpActionResult> Register(RegisterBindingModel model) { var message = ""; if (!ModelState.IsValid) { return BadRequest(ModelState); } //Check e-mail address if (!regExUtilities.IsValidEmail(model.Email)) { message = "The e-mail address is not in a valid format. Please re-enter it."; ModelState.AddModelError("Email Error", message); return BadRequest(ModelState); } else if (db.Users.Any(u => u.Email == model.Email)) { message = "That e-mail address is already registered. Please register with a diffrent one."; ModelState.AddModelError("Email Error", message); return BadRequest(ModelState); } else { model.Username = model.Email; } if (!model.CreateHousehold) { if (db.Invitations.Any(i => i.InviteCode == model.InviteCode)) { var sendingUserId = db.Invitations.FirstOrDefault(i => i.InviteCode == model.InviteCode).FromUserId; if (db.Users.Any(u => u.Id == sendingUserId)) { model.Household = db.Users.FirstOrDefault(u => u.Id == sendingUserId).Household; Invitation usedInvite = db.Invitations.FirstOrDefault(i => i.InviteCode == model.InviteCode); db.Invitations.Remove(usedInvite); db.SaveChanges(); } else { message = "The user who sent the invitation is no longer a member of the household. " + "Please register with a new household name, or request an e-mail invititation " + "from a current member of an existing household."; ModelState.AddModelError("Invitation Error", message); return BadRequest(ModelState); } } else { message = "The invitation code entered is invalid or has been used already. " + "Please double-check it and try again. Note that the code is case sensitive."; ModelState.AddModelError("Invitation Error", message); return BadRequest(ModelState); } } var user = new ApplicationUser() { FirstName = model.FirstName, LastName = model.LastName, UserName = model.Username, Email = model.Email, Household = model.Household }; if (model.CreateHousehold) { if (model.Household == "") { message = "The household name must be at least one charachter long. Please choose a different one."; ModelState.AddModelError("Household Error", message); return BadRequest(ModelState); } else if (db.Users.Any(u => u.Household == model.Household)) { message = "Household '" + model.Household + "' already exists. Please choose a different Household name, or request an e-mail invitation to '" + model.Household + "' from a current member."; ModelState.AddModelError("Household Error", message); return BadRequest(ModelState); } else { user.Household = model.Household; } } IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return GetErrorResult(result); } return Ok(); }