public IActionResult RegisterUser(RegUser newRegUser)
        {
            if (ModelState.IsValid)
            {
                User current = _context.Users.SingleOrDefault(e => e.Email == newRegUser.Email);
                if (current != null)
                {
                    ModelState.AddModelError("Email", "Email already exists!");

                    return(View("Index"));
                }
                else
                {
                    PasswordHasher <RegUser> Hasher = new PasswordHasher <RegUser>();
                    string hashed = Hasher.HashPassword(newRegUser, newRegUser.Password);
                    User   user   = new User // Creating a new User using the info provided by the user via the form
                    {
                        FirstName = newRegUser.FirstName,
                        LastName  = newRegUser.LastName,
                        Email     = newRegUser.Email,
                        Password  = hashed,
                    };
                    _context.Add(user);
                    _context.SaveChanges();
                    User sessionuser = _context.Users.Where(u => u.Email == newRegUser.Email).SingleOrDefault();
                    HttpContext.Session.SetInt32("userID", sessionuser.UserId);
                    HttpContext.Session.SetString("firstname", sessionuser.FirstName);
                    return(RedirectToAction("Dash"));
                }
            }
            else
            {
                return(View("Index"));
            }
        }
Exemple #2
0
 public IActionResult RegisterUser(RegUser newRegUser)                                    // This Method takes in a parameter of newRegUser
 {
     if (ModelState.IsValid)                                                              // There are NO errors
     {
         User current = _context.Users.SingleOrDefault(e => e.Email == newRegUser.Email); // Query to check if there is an existing email
         if (current != null)                                                             // If the results come up with something...
         {
             ModelState.AddModelError("Email", "Email already exists!");                  // ...An email exists and renders the Index View
             // This is an example of another way to add errors without using ViewBag
             // The parameters: ModelState.AddModelError("The attribute NAME on the form where you want to put it", "Error Message");
             return(View("Index"));
         }
         else // If email does not exist in the database yet...
         {
             PasswordHasher <RegUser> Hasher = new PasswordHasher <RegUser>(); // This will hash the password
             string hashed = Hasher.HashPassword(newRegUser, newRegUser.Password); // This will take in the user's given password and hash it
             User   user   = new User                                              // Creating a new User using the info provided by the user via the form
             {
                 FirstName = newRegUser.FirstName,
                 LastName  = newRegUser.LastName,
                 Email     = newRegUser.Email,
                 Password  = hashed,
             };
             _context.Add(user);                                                                          // Instant Query to ADD!
             _context.SaveChanges();                                                                      // Query to Save Changes
             User sessionuser = _context.Users.Where(u => u.Email == newRegUser.Email).SingleOrDefault(); // Model User has a variable sessionuser that contains the query of getting the new user's email to gain access to their other info
             HttpContext.Session.SetInt32("userID", sessionuser.UserId);                                  // Setting userID to hold onto the user's UserId while in session
             HttpContext.Session.SetString("firstname", sessionuser.FirstName);                           // Setting firstname to hold th user's FirstName while in session
             return(RedirectToAction("Dash"));                                                            // Goes to the Method named Dash
         }
     }
     else // If there ARE errors present...
     {
         return(View("Index")); // ...Go back to the Index view
         // When you want to show errors, DO NOT Redirect!
     }
 }