public ActionResult ConfirmRegisterGoogle(GoogleAccountModel model)
 {
     
     User user = UserHelpers.GetCurrentUser(Session);
     if (user != null)
     {
         TempData["errorTittle"] = "Bad request";
         TempData["errorMessage"] = "You are already signed in the system";
         return RedirectToAction("Index", "Home");
     }
     TempData["errorTitle"] = null;
     TempData["errorMessage"] = null;
     return View(model);
 }
 public ActionResult ExternalLoginConfirmation(GoogleAccountModel model)
 {
     User user = UserHelpers.GetCurrentUser(Session);
     if (user != null) {
         TempData["errorTittle"] = "Bad request";
         TempData["errorMessage"] = "You are already signed in the system";
         return RedirectToAction("Index", "Home");
     }
     if (ModelState.IsValid)
     {
         User newUser = UserDatabaseHelper.Instance.GetUserByUserName(model.UserName);
         if (newUser!= null)
         {
             ModelState.AddModelError("", "UserName is already exist. Please choose another.");
             TempData["errorTitle"] = null;
             TempData["errorMessage"] = null;
             return View("ConfirmRegisterGoogle", model);
         }
         newUser = new User();
         newUser.UserEmail = model.Email;
         newUser.UserName = model.UserName;
         newUser.UserPassword = model.Password;
         newUser.UserDOB = model.UserDOB;
         newUser.Place = model.Place;
         newUser.UserFirstName = model.UserFirstName;
         newUser.DataJoin = DateTime.Today;
         if (model.UserLastName != null && model.UserLastName != "")
         {
             newUser.UserLastName = model.UserLastName;
         }
         newUser.AccountStatus = EventZoneConstants.ActiveUser; //set Active account
         newUser.Avartar = 10032;
         newUser.UserRoles = EventZoneConstants.User; //set UserRole
         // insert user to Database
         db.Users.Add(newUser);
         db.SaveChanges();
         UserHelpers.SetCurrentUser(Session, newUser);
         //Send email confirm
         MailHelpers.Instance.SendMailWelcome(newUser.UserEmail, newUser.UserFirstName, newUser.UserLastName);
         TempData["errorTitle"] = "Sucessfull SignUp";
         TempData["errorMessage"] = "Thank you for signing up in EventZone! We sent you a welcome message! Hope you have more fun and comfortable by joining with us";
         return RedirectToAction("Index", "Home");
     }
     TempData["errorTitle"] = "Invald input";
     TempData["errorMessage"] = "Invalid input! Please try again";
     // If we got this far, something failed, redisplay form
     return RedirectToAction("ExternalLoginConfirmation", "Account",model);
 }
 public ActionResult GoogleCallback()
 {
     try
     {
         if (!string.IsNullOrEmpty(Request.QueryString["code"]))
         {
             var code = Request.QueryString["code"];
             dynamic google = JObject.Parse(GoogleConnect.Fetch("me", code));
             var emailList = new JArray(google.emails);
             var email = "";
             foreach (var x in emailList)
             {
                 var e = x.ToObject<Email>();
                 if (e.Type.Equals("account"))
                 {
                     email = e.Value;
                 }
             }
             
             // select from DB
             var newUser = UserDatabaseHelper.Instance.GetUserByEmail(email);
             //if this is first time login
             if (newUser == null)
             {
                 var addressList = new JArray();
                 if (google.placesLived != null)
                 {
                     addressList = new JArray(google.placesLived);
                 }
                 var address = "";
                 foreach (var x in addressList)
                 {
                     var a = x.ToObject<Address>();
                     if (a.Primary)
                     {
                         address = a.Value;
                     }
                 }
                 var ggModel = new GoogleAccountModel
                 {
                     Email = email,
                     Place = address,
                     UserFirstName = google.name.familyName.Value,
                     UserLastName = google.name.givenName.Value
                     //Gender = google.gender == null ? 0 : google.gender.Value   
                 };
                 return View("ConfirmRegisterGoogle", ggModel);
             }
             if (UserDatabaseHelper.Instance.isLookedUser(newUser.UserName))
             {
                 // user is Locked
                 GoogleConnect.Clear();
                 TempData["errorTitle"] = "Locked user";
                 TempData["errorMessage"] = "Ops...Your account has been locked! Please contact with our support!";
                 return RedirectToAction("Index", "Home");
             }
             TempData["errorTitle"] = null;
             TempData["errorMessage"] = null;
             UserHelpers.SetCurrentUser(Session, newUser);
             return RedirectToAction("Index", "Home");
         }
         else if (!string.IsNullOrEmpty(Request.QueryString["error"]))
         {
             TempData["errorTitle"] = "Signin Error";
             TempData["errorMessage"] = Request.QueryString["error"].ToString();
             return RedirectToAction("Index", "Home");
         }
         else {
             if (Request.Cookies["userName"] != null)
             {
                 HttpCookie userName = Request.Cookies["userName"];
                 userName.Expires = DateTime.Now.AddDays(-1);
                 Response.Cookies.Add(userName);
             }
             //remove cookie password
             if (Request.Cookies["password"] != null)
             {
                 HttpCookie password = Request.Cookies["password"];
                 password.Expires = DateTime.Now.AddDays(-1);
                 Request.Cookies.Add(password);
             }
             UserHelpers.SetCurrentUser(Session, null);
             return RedirectToAction("Index", "Home");
         }
        
     }
     catch
     {
         if (Request.Cookies["userName"] != null)
         {
             HttpCookie userName = Request.Cookies["userName"];
             userName.Expires = DateTime.Now.AddDays(-1);
             Response.Cookies.Add(userName);
         }
         //remove cookie password
         if (Request.Cookies["password"] != null)
         {
             HttpCookie password = Request.Cookies["password"];
             password.Expires = DateTime.Now.AddDays(-1);
             Request.Cookies.Add(password);
         }
         TempData["errorTitle"] = "Social Signin Error";
         TempData["errorMessage"] = "Failed to connect with Google! Check your connection please...";
         UserHelpers.SetCurrentUser(Session, null);
         return RedirectToAction("Index", "Home");
     }
 }