feat: add registration and login validation

This commit is contained in:
cuqmbr 2022-04-01 14:56:21 +03:00
parent 8c9a0aeae3
commit 0472075a7b
4 changed files with 156 additions and 20 deletions

View File

@ -14,12 +14,16 @@
Авторизація
</div>
<input class="field" placeholder="E-mail" autocomplete="off" asp-for="User.Email"/>
<span asp-validation-for="User.Email" class="validation-error"></span>
<input class="field" type="text" placeholder="E-mail" autocomplete="off" asp-for="Email"/>
<div class="validation-error">
<span>@Model.emailValidation</span>
</div>
<br>
<input class="field" placeholder="Пароль" autocomplete="off" asp-for="User.Password"/>
<span asp-validation-for="User.Password" class="validation-error"></span>
<input class="field" type="password" placeholder="Пароль" autocomplete="off" asp-for="Password"/>
<div class="validation-error">
<span>@Model.passwordValidation</span>
</div>
<br>
<input class="submit-btn" type="submit" value="Авторизуватись"/>

View File

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

View File

@ -14,12 +14,16 @@
Реєстрація
</div>
<input class="field" placeholder="E-mail" autocomplete="off" asp-for="User.Email"/>
<span asp-validation-for="User.Email" class="validation-error"></span>
<input class="field" type="text" placeholder="E-mail" autocomplete="off" asp-for="Email"/>
<div class="validation-error">
<span>@Model.emailValidation</span>
</div>
<br>
<input class="field" placeholder="Пароль" autocomplete="off" asp-for="User.Password"/>
<span asp-validation-for="User.Password" class="validation-error"></span>
<input class="field" type="password" placeholder="Пароль" autocomplete="off" asp-for="Password"/>
<div class="validation-error">
<span>@Model.passwordValidation</span>
</div>
<br>
<input class="submit-btn" type="submit" value="Зареєструватись"/>

View File

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