public async Task <IActionResult> Register(RegisterViewModel reg) { if (ModelState.IsValid) { // Check if username is not taken if (!await AccountDb.IsUsernameAvailable(reg.Username, _context)) { Account acc = new Account() { Email = reg.Email, FullName = reg.FullName, Password = reg.Password, Username = reg.Username }; // Add Account to DB await AccountDb.Register(_context, acc); SessionHelper.CreateUserSession(acc.AccountId, acc.Username, _http); return(RedirectToAction("Index", "Home")); } else // If username is taken, add error { // Display error with other username error messages ModelState.AddModelError(nameof(Account.Username), "Username is already taken"); } } return(View(reg)); }
public async Task <IActionResult> Register(RegisterViewModel reg) { if (ModelState.IsValid) { //Check username is unique if (!await AccountDb.IsUsernameAvailable(reg.Username, _context)) { Account acc = new Account() { Email = reg.Email, FullName = reg.FullName, Password = reg.Password, Username = reg.Username }; //if unique, add account to DB await AccountDb.Register(_context, acc); //with the DB made, this links the session CREATE USER SESSION SessionHelper.CreateUserSession(acc.AccountId, acc.Username, _http); return(RedirectToAction("Index", "Home")); } else // If username is taken, add error { // Display error with other username error messages //Tell it what class this property is in ModelState.AddModelError(nameof(Account.Username) , "Username is already taken"); } } return(View(reg)); }