public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                //check is user already exists
                var user = UserManager.FindByEmail(model.Email);
                if (user == null)
                {
                    //user does not exist, create user
                    user = new ApplicationUser(); 
                    user.UserName = model.UserName; 
                    user.Email = model.Email;
                    user.FirstName = model.FirstName; 
                    user.LastName = model.LastName;

                    if (model.HouseholdId.HasValue)
                    {
                        user.HouseholdId = model.HouseholdId.Value;
                    }
                    else
                    {
                        //creates new household on registration
                        Household household = new Household();
                        household.Name = model.HouseholdName;

                        db.Households.Add(household);
                        db.SaveChanges();

                        
                        var newHousehold = db.Households.OrderByDescending(h => h.Id).First();
                        user.HouseholdId = newHousehold.Id;

                    }

                    var result = await UserManager.CreateAsync(user, model.Password);


                    if (!result.Succeeded) 
                    {
                        //user couldn't be created for some reason
                        return View("Register", model);
                    }                    
                }
                //we have a user, either new or existing. so sign them in.
                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                //check for household ID, only coming from join page
                if (model.HouseholdId != null)
                {
                    var db = new ApplicationDbContext();
                    //we have a householdso get the recently created /signed in user
                    user = db.Users.Single(u => u.UserName == model.Email);
                    //find household matching ID
                    var h = db.Households.Find(model.HouseholdId);
                    //add the household to the user's list of households
                    user.Household = h;
                    //save changes
                    db.SaveChanges();
                }
                return RedirectToAction("Index", "Home");                                                  
            }            
            // If we got this far, something failed, redisplay form
            return View(model);
        }
 public async Task<ActionResult> JoinHousehold(string JoinCode)
 {
     //set up the confirmation page so the user can confirm he wants to change households
     //might also need a view model for this page
     var invitation = await db.Invitations.FirstOrDefaultAsync(i => i.JoinCode.ToString() == JoinCode);
     if (JoinCode == invitation.JoinCode.ToString())
     {
         var model = new RegisterViewModel
         {
             HouseholdId = invitation.Household.Id,
             HouseholdName = invitation.Household.Name,
             Email = invitation.ToEmail
         };
         var user = db.Users.FirstOrDefault(u => u.Email == invitation.ToEmail);
         if (user != null){
             //user exists in the system populate the rest of the user info
             model.FirstName = user.FirstName;
             model.LastName = user.LastName;
         }
         return View(model);
     }
     else
     {
         return RedirectToAction("Login", "Account", new { message = "Unauthorized" });
     }
 }