refactor: code cleanup on Auth pages

This commit is contained in:
cuqmbr 2022-05-12 18:33:11 +03:00
parent 3bf86ff66e
commit 7b03b7754c
4 changed files with 32 additions and 42 deletions

View File

@ -14,15 +14,15 @@
Авторизація Авторизація
</div> </div>
<input class="field" type="text" placeholder="E-mail" autocomplete="off" asp-for="Email"/> <input class="field" type="text" placeholder="E-mail" autocomplete="off" asp-for="User.Email"/>
<div class="validation-error"> <div class="validation-error">
<span>@Model.EmailValidation</span> <span>@Model.EmailValidationError</span>
</div> </div>
<br> <br>
<input class="field" type="password" placeholder="Пароль" autocomplete="off" asp-for="Password"/> <input class="field" type="password" placeholder="Пароль" autocomplete="off" asp-for="User.Password"/>
<div class="validation-error"> <div class="validation-error">
<span>@Model.PasswordValidation</span> <span>@Model.PasswordValidationError</span>
</div> </div>
<br> <br>

View File

@ -9,13 +9,10 @@ namespace TicketOffice.Pages.Auth;
public class LoginModel : PageModel public class LoginModel : PageModel
{ {
[BindProperty] public string Email { get; set; } = String.Empty; [BindProperty] public User? User { get; set; }
[BindProperty] public string Password { get; set; } = String.Empty;
public string EmailValidation; public string EmailValidationError;
public string PasswordValidation; public string PasswordValidationError;
private List<User> User { get; set; }
private readonly TicketOfficeContext _context; private readonly TicketOfficeContext _context;
@ -30,7 +27,10 @@ public class LoginModel : PageModel
{ {
if (ValidateForm()) 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"); return RedirectToPage("/Auth/Account");
} }
@ -39,15 +39,13 @@ public class LoginModel : PageModel
private bool ValidateForm() private bool ValidateForm()
{ {
User = _context.User User? user = _context.User.FirstOrDefault(u => u.Email == User.Email);
.Where(u => u.Email == Email)
.ToList();
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) bool ValidateEmail(string email, out string validationError)
{ {
if (User.Count == 1) if (user is not null)
{ {
validationError = String.Empty; validationError = String.Empty;
return true; return true;
@ -73,7 +71,7 @@ public class LoginModel : PageModel
bool ValidatePassword(string password, out string validationError) bool ValidatePassword(string password, out string validationError)
{ {
if (User.First().Password == password) if (user.Password == password)
{ {
validationError = String.Empty; validationError = String.Empty;
return true; return true;

View File

@ -14,15 +14,15 @@
Реєстрація Реєстрація
</div> </div>
<input class="field" type="text" placeholder="E-mail" autocomplete="off" asp-for="Email"/> <input class="field" type="text" placeholder="E-mail" autocomplete="off" asp-for="User.Email"/>
<div class="validation-error"> <div class="validation-error">
<span>@Model.EmailValidation</span> <span>@Model.EmailValidationError</span>
</div> </div>
<br> <br>
<input class="field" type="password" placeholder="Пароль" autocomplete="off" asp-for="Password"/> <input class="field" type="password" placeholder="Пароль" autocomplete="off" asp-for="User.Password"/>
<div class="validation-error"> <div class="validation-error">
<span>@Model.PasswordValidation</span> <span>@Model.PasswordValidationError</span>
</div> </div>
<br> <br>

View File

@ -9,13 +9,10 @@ namespace TicketOffice.Pages.Auth;
public class RegistrationModel : PageModel public class RegistrationModel : PageModel
{ {
[BindProperty] public string Email { get; set; } = String.Empty; [BindProperty] public User User { get; set; }
[BindProperty] public string Password { get; set; } = String.Empty;
public string EmailValidation; public string EmailValidationError;
public string PasswordValidation; public string PasswordValidationError;
private List<User> User { get; set; }
private readonly TicketOfficeContext _context; private readonly TicketOfficeContext _context;
@ -26,19 +23,16 @@ public class RegistrationModel : PageModel
public ActionResult OnGet() => ValidateSession() ? RedirectToPage("/Auth/Account") : Page(); public ActionResult OnGet() => ValidateSession() ? RedirectToPage("/Auth/Account") : Page();
public ActionResult OnPostAsync() public ActionResult OnPost()
{ {
if (ValidateForm()) if (ValidateForm())
{ {
_context.User.Add(new User {Email = Email, Password = Password}); _context.User.Add(User);
_context.SaveChanges(); _context.SaveChanges();
User = _context.User User = _context.User.FirstOrDefault(u => u.Email == User.Email);
.Where(u => u.Email == Email)
.ToList();
HttpContext.Session.SetInt32("UserId", User.First().Id); HttpContext.Session.SetInt32("UserId", User.Id);
return RedirectToPage("/Auth/Account"); return RedirectToPage("/Auth/Account");
} }
@ -48,7 +42,7 @@ public class RegistrationModel : PageModel
private bool ValidateForm() 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) bool ValidateEmail(string email, out string validationError)
{ {
@ -66,11 +60,9 @@ public class RegistrationModel : PageModel
return false; return false;
} }
User = _context.User User user = _context.User.FirstOrDefault(u => u.Email == User.Email);
.Where(u => u.Email == Email)
.ToList();
if (User.Any()) if (user is not null)
{ {
validationError = "E-mail уже зареєстровано"; validationError = "E-mail уже зареєстровано";
return false; return false;