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>
<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">
<span>@Model.EmailValidation</span>
<span>@Model.EmailValidationError</span>
</div>
<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">
<span>@Model.PasswordValidation</span>
<span>@Model.PasswordValidationError</span>
</div>
<br>

View File

@ -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> 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;

View File

@ -14,15 +14,15 @@
Реєстрація
</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">
<span>@Model.EmailValidation</span>
<span>@Model.EmailValidationError</span>
</div>
<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">
<span>@Model.PasswordValidation</span>
<span>@Model.PasswordValidationError</span>
</div>
<br>

View File

@ -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> 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
.Where(u => u.Email == Email)
.ToList();
User = _context.User.FirstOrDefault(u => u.Email == User.Email);
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)
{
@ -66,11 +60,9 @@ public class RegistrationModel : PageModel
return false;
}
User = _context.User
.Where(u => u.Email == Email)
.ToList();
User user = _context.User.FirstOrDefault(u => u.Email == User.Email);
if (User.Any())
if (user is not null)
{
validationError = "E-mail уже зареєстровано";
return false;