diff --git a/TicketOffice/Pages/Auth/Login.cshtml b/TicketOffice/Pages/Auth/Login.cshtml index 0f46d9e..e8919c5 100644 --- a/TicketOffice/Pages/Auth/Login.cshtml +++ b/TicketOffice/Pages/Auth/Login.cshtml @@ -14,15 +14,15 @@ Авторизація - +
- @Model.EmailValidation + @Model.EmailValidationError

- +
- @Model.PasswordValidation + @Model.PasswordValidationError

diff --git a/TicketOffice/Pages/Auth/Login.cshtml.cs b/TicketOffice/Pages/Auth/Login.cshtml.cs index dcb275c..8ca1a21 100644 --- a/TicketOffice/Pages/Auth/Login.cshtml.cs +++ b/TicketOffice/Pages/Auth/Login.cshtml.cs @@ -9,13 +9,10 @@ namespace TicketOffice.Pages.Auth; public class LoginModel : PageModel { - [BindProperty] public string Email { get; set; } = String.Empty; - [BindProperty] public string Password { get; set; } = String.Empty; + [BindProperty] public User? User { get; set; } - public string EmailValidation; - public string PasswordValidation; - - private List User { get; set; } + public string EmailValidationError; + public string PasswordValidationError; private readonly TicketOfficeContext _context; @@ -30,7 +27,10 @@ public class LoginModel : PageModel { if (ValidateForm()) { - HttpContext.Session.SetInt32("UserId", User.First().Id); + User user = _context.User.FirstOrDefault(u => u.Email == User.Email); + + HttpContext.Session.SetInt32("UserId", user.Id); + HttpContext.Session.SetInt32("IsManager", user.IsManager ? 1 : 0); return RedirectToPage("/Auth/Account"); } @@ -39,15 +39,13 @@ public class LoginModel : PageModel private bool ValidateForm() { - User = _context.User - .Where(u => u.Email == Email) - .ToList(); + User? user = _context.User.FirstOrDefault(u => u.Email == User.Email); - return ValidateEmail(Email, out EmailValidation) && ValidatePassword(Password, out PasswordValidation); + return ValidateEmail(User.Email, out EmailValidationError) && ValidatePassword(User.Password, out PasswordValidationError); bool ValidateEmail(string email, out string validationError) { - if (User.Count == 1) + if (user is not null) { validationError = String.Empty; return true; @@ -73,7 +71,7 @@ public class LoginModel : PageModel bool ValidatePassword(string password, out string validationError) { - if (User.First().Password == password) + if (user.Password == password) { validationError = String.Empty; return true; diff --git a/TicketOffice/Pages/Auth/Registration.cshtml b/TicketOffice/Pages/Auth/Registration.cshtml index c41abbc..756bb5d 100644 --- a/TicketOffice/Pages/Auth/Registration.cshtml +++ b/TicketOffice/Pages/Auth/Registration.cshtml @@ -14,15 +14,15 @@ Реєстрація - +
- @Model.EmailValidation + @Model.EmailValidationError

- +
- @Model.PasswordValidation + @Model.PasswordValidationError

diff --git a/TicketOffice/Pages/Auth/Registration.cshtml.cs b/TicketOffice/Pages/Auth/Registration.cshtml.cs index 9a74067..0947e0b 100644 --- a/TicketOffice/Pages/Auth/Registration.cshtml.cs +++ b/TicketOffice/Pages/Auth/Registration.cshtml.cs @@ -9,13 +9,10 @@ namespace TicketOffice.Pages.Auth; public class RegistrationModel : PageModel { - [BindProperty] public string Email { get; set; } = String.Empty; - [BindProperty] public string Password { get; set; } = String.Empty; + [BindProperty] public User User { get; set; } - public string EmailValidation; - public string PasswordValidation; - - private List User { get; set; } + public string EmailValidationError; + public string PasswordValidationError; private readonly TicketOfficeContext _context; @@ -26,19 +23,16 @@ public class RegistrationModel : PageModel public ActionResult OnGet() => ValidateSession() ? RedirectToPage("/Auth/Account") : Page(); - public ActionResult OnPostAsync() + public ActionResult OnPost() { if (ValidateForm()) { - _context.User.Add(new User {Email = Email, Password = Password}); - + _context.User.Add(User); _context.SaveChanges(); + + User = _context.User.FirstOrDefault(u => u.Email == User.Email); - User = _context.User - .Where(u => u.Email == Email) - .ToList(); - - HttpContext.Session.SetInt32("UserId", User.First().Id); + HttpContext.Session.SetInt32("UserId", User.Id); return RedirectToPage("/Auth/Account"); } @@ -48,7 +42,7 @@ public class RegistrationModel : PageModel private bool ValidateForm() { - return ValidateEmail(Email, out EmailValidation) && ValidatePassword(Password, out PasswordValidation); + return ValidateEmail(User.Email, out EmailValidationError) && ValidatePassword(User.Password, out PasswordValidationError); bool ValidateEmail(string email, out string validationError) { @@ -65,12 +59,10 @@ public class RegistrationModel : PageModel validationError = "E-mail некоректний"; return false; } - - User = _context.User - .Where(u => u.Email == Email) - .ToList(); - - if (User.Any()) + + User user = _context.User.FirstOrDefault(u => u.Email == User.Email); + + if (user is not null) { validationError = "E-mail уже зареєстровано"; return false;