upload
This commit is contained in:
53
Views/LoginWindow.xaml.cs
Normal file
53
Views/LoginWindow.xaml.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.Windows;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SimpleWiki.Data;
|
||||
using SimpleWiki.Services;
|
||||
|
||||
namespace SimpleWiki.Views;
|
||||
|
||||
public partial class LoginWindow : Window
|
||||
{
|
||||
public LoginWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void LoginButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var emailError = InputValidator.ValidateEmail(EmailTextBox.Text);
|
||||
var passwordError = string.IsNullOrWhiteSpace(PasswordBox.Password) ? "Пароль обязателен." : null;
|
||||
var errors = InputValidator.JoinErrors(emailError, passwordError);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(errors))
|
||||
{
|
||||
MessageBox.Show(errors, "Ошибка валидации", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
using var db = new AppDbContext();
|
||||
var email = EmailTextBox.Text.Trim().ToLowerInvariant();
|
||||
var user = db.Users.AsNoTracking().FirstOrDefault(x => x.Email.ToLower() == email);
|
||||
|
||||
if (user is null || !PasswordHasher.VerifyPassword(PasswordBox.Password, user.PasswordHash))
|
||||
{
|
||||
MessageBox.Show("Неверный email или пароль.", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
SessionService.CurrentUser = user;
|
||||
new MainWindow(user).Show();
|
||||
Close();
|
||||
}
|
||||
|
||||
private void OpenRegisterButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
new RegisterWindow().Show();
|
||||
Close();
|
||||
}
|
||||
|
||||
private void OpenForgotPasswordButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
new ForgotPasswordWindow().Show();
|
||||
Close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user