c969-project/C969Project/LoginForm.cs
2025-06-14 22:52:42 -05:00

63 lines
2.1 KiB
C#

using C969Project.Data;
using C969Project.Data.Models;
namespace C969Project
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
loginLabel.Text = Localization.GetLocalization("login_text");
usernameTextBox.PlaceholderText = Localization.GetLocalization("username_text");
passwordTextBox.PlaceholderText = Localization.GetLocalization("password_text");
loginButton.Text = Localization.GetLocalization("login_text");
loginButton.Enabled = false;
usernameTextBox.TextChanged += (sender, args) => {UpdateLoginButtonState();};
passwordTextBox.TextChanged += (sender, args) => {UpdateLoginButtonState();};
loginButton.Click += LoginButtonOnClick;
}
private void LoginButtonOnClick(object? sender, EventArgs e)
{
var usr = DatabaseHelper.Login(usernameTextBox.Text, passwordTextBox.Text);
if (usr is null)
{
MessageBox.Show(Localization.GetLocalization("login_error_message"), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
LogHistory(usr);
Console.WriteLine($"Login successful, {usr.Username}");
AppState.CurrentUser = usr;
var dash = new DashboardForm();
dash.FormClosing += (o, args) => { Close(); };
dash.Show();
Hide();
}
private void UpdateLoginButtonState()
{
if (string.IsNullOrEmpty(usernameTextBox.Text) || string.IsNullOrEmpty(passwordTextBox.Text))
{
loginButton.Enabled = false;
}
else
{
loginButton.Enabled = true;
}
}
private void LogHistory(User user)
{
string text = $"{DateTime.UtcNow} UTC : {user.Username}";
using StreamWriter sw = new StreamWriter("Login_History.txt", true);
sw.WriteLine(text);
}
}
}