diff --git a/TicketOffice/Pages/Auth/Login.cshtml b/TicketOffice/Pages/Auth/Login.cshtml index 26666d9..b6a1902 100644 --- a/TicketOffice/Pages/Auth/Login.cshtml +++ b/TicketOffice/Pages/Auth/Login.cshtml @@ -14,12 +14,16 @@ Авторизація - - + +
+ @Model.emailValidation +

- - + +
+ @Model.passwordValidation +

diff --git a/TicketOffice/Pages/Auth/Login.cshtml.cs b/TicketOffice/Pages/Auth/Login.cshtml.cs index 9fe29b6..1af14b8 100644 --- a/TicketOffice/Pages/Auth/Login.cshtml.cs +++ b/TicketOffice/Pages/Auth/Login.cshtml.cs @@ -1,5 +1,7 @@ +using System.Text.RegularExpressions; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.EntityFrameworkCore; using TicketOffice.Data; using TicketOffice.Models; @@ -7,6 +9,12 @@ namespace TicketOffice.Pages.Auth; public class LoginModel : PageModel { + public IList User { get; set; } + [BindProperty] public string Email { get; set; } + [BindProperty] public string Password { get; set; } + public string emailValidation; + public string passwordValidation; + private readonly TicketOfficeContext _context; public LoginModel(TicketOfficeContext context) @@ -19,12 +27,64 @@ public class LoginModel : PageModel return Page(); } - [BindProperty] - public User User { get; set; } - public async Task OnPostAsync() { - //Login logic + emailValidation = String.Empty; + passwordValidation = String.Empty; + + User = await _context.User + .Where(u => u.Email == Email) + .ToListAsync(); + + if (ValidateEmail(Email, out emailValidation) && ValidatePassword(Password, out passwordValidation)) + { + return RedirectToPage("/Index"); + } + return Page(); } + + public bool ValidateEmail(string email, out string validationError) + { + if (User.Any(u => u.Email == email)) + { + validationError = String.Empty; + return true; + } + + if (String.IsNullOrWhiteSpace(email)) + { + validationError = "Поле має бути заповненим"; + return false; + } + + Regex emailRegex = new Regex(@"^[^@\s]+@[^@\s]+\.[^@\s]+$"); + + if (!emailRegex.IsMatch(email)) + { + validationError = "E-mail некоректний"; + return false; + } + + validationError = "E-mail не зареєстровано"; + return false; + } + + public bool ValidatePassword(string password, out string validationError) + { + if (User.Where(u => u.Email == Email).Any(u => u.Password == password)) + { + validationError = String.Empty; + return true; + } + + if (String.IsNullOrWhiteSpace(password)) + { + validationError = "Поле має бути заповненим"; + return false; + } + + validationError = "Неправильний пароль"; + return false; + } } \ No newline at end of file diff --git a/TicketOffice/Pages/Auth/Registration.cshtml b/TicketOffice/Pages/Auth/Registration.cshtml index 48481fb..4d68898 100644 --- a/TicketOffice/Pages/Auth/Registration.cshtml +++ b/TicketOffice/Pages/Auth/Registration.cshtml @@ -14,12 +14,16 @@ Реєстрація - - + +
+ @Model.emailValidation +

- - + +
+ @Model.passwordValidation +

diff --git a/TicketOffice/Pages/Auth/Registration.cshtml.cs b/TicketOffice/Pages/Auth/Registration.cshtml.cs index 683c334..2ca672e 100644 --- a/TicketOffice/Pages/Auth/Registration.cshtml.cs +++ b/TicketOffice/Pages/Auth/Registration.cshtml.cs @@ -1,5 +1,7 @@ +using System.Text.RegularExpressions; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.EntityFrameworkCore; using TicketOffice.Data; using TicketOffice.Models; @@ -7,6 +9,12 @@ namespace TicketOffice.Pages.Auth; public class RegistrationModel : PageModel { + public IList User { get; set; } + [BindProperty] public string Email { get; set; } + [BindProperty] public string Password { get; set; } + public string emailValidation; + public string passwordValidation; + private readonly TicketOfficeContext _context; public RegistrationModel(TicketOfficeContext context) @@ -16,22 +24,82 @@ public class RegistrationModel : PageModel public IActionResult OnGet() { + emailValidation = String.Empty; + passwordValidation = String.Empty; + return Page(); } - [BindProperty] - public User User { get; set; } - public async Task OnPostAsync() { - if (!ModelState.IsValid) + User = await _context.User + .Where(u => u.Email == Email) + .ToListAsync(); + + if (ValidateEmail(Email, out emailValidation) && ValidatePassword(Password, out passwordValidation)) { - return Page(); + _context.User.Add(new User + { + Email = Email, + Password = Password + }); + await _context.SaveChangesAsync(); + + return RedirectToPage("/Index"); } - _context.User.Add(User); - await _context.SaveChangesAsync(); + return Page(); + } + + public bool ValidateEmail(string email, out string validationError) + { + Regex emailRegex = new Regex(@"^[^@\s]+@[^@\s]+\.[^@\s]+$"); - return RedirectToPage("./"); + if (String.IsNullOrWhiteSpace(email)) + { + validationError = "Поле має бути заповненим"; + return false; + } + + if (!emailRegex.IsMatch(email)) + { + validationError = "E-mail некоректний"; + return false; + } + + if (User.Any()) + { + validationError = "E-mail уже зареєстровано"; + return false; + } + + validationError = String.Empty; + return true; + } + + public bool ValidatePassword(string passowrd, out string validationError) + { + if (String.IsNullOrWhiteSpace(passowrd)) + { + validationError = "Поле має бути заповненим"; + return false; + } + + if (passowrd.Length < 8 || passowrd.Length > 32) + { + validationError = "Паороль має бути від 8 до 32 символів"; + return false; + } + + Regex passwordRegex = new Regex(@"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$"); + + if (!passwordRegex.IsMatch(passowrd)) + { + validationError = "Пароль має містити великі та малі латинські літери, цифри та спеціальні знаки (@, $, % та ін.)"; + return false; + } + + validationError = String.Empty; + return true; } } \ No newline at end of file