mirror of
https://github.com/ultrasn0w/app.git
synced 2025-12-13 12:19:54 +01:00
126 lines
3.9 KiB
C#
126 lines
3.9 KiB
C#
using APP.DTO;
|
|
using APP.Utility;
|
|
|
|
namespace APP;
|
|
|
|
public partial class RegisterWin : Form
|
|
{
|
|
private readonly Auth? _prevAuth;
|
|
private readonly List<User>? _users;
|
|
private bool _exist;
|
|
private bool _uChecked;
|
|
private User? _user;
|
|
|
|
public Auth Auth;
|
|
public bool Cancelled;
|
|
|
|
public RegisterWin(Auth? prevAuth, List<User>? existing)
|
|
{
|
|
_prevAuth = prevAuth;
|
|
_user = new User();
|
|
_users = existing;
|
|
Cancelled = true;
|
|
Auth = new Auth();
|
|
InitializeComponent();
|
|
Load += OnLoadEv;
|
|
}
|
|
|
|
private async Task<bool> PerformRegister(User user, Auth auth)
|
|
{
|
|
if (_exist)
|
|
{
|
|
// only try change login
|
|
return await BackendComms.Auth(auth.Username, auth.Secret);
|
|
}
|
|
|
|
var register = new Register
|
|
{
|
|
Username = user.Username,
|
|
MalId = user.Id,
|
|
Secret = auth.Secret,
|
|
Sauce = StringAssemble.CalcSauce(user.Id, user.Username)
|
|
};
|
|
var res = await BackendComms.Register(register);
|
|
return res != null;
|
|
}
|
|
|
|
private void OnLoadEv(object? sender, EventArgs e)
|
|
{
|
|
// check if we exist
|
|
if (_users == null || _users.All(u => u.Username != _prevAuth?.Username)) return;
|
|
_exist = true;
|
|
usernameBox.Name = _prevAuth?.Username;
|
|
}
|
|
|
|
private void cancelButton_Click(object sender, EventArgs e)
|
|
{
|
|
Cancelled = true;
|
|
Close();
|
|
}
|
|
|
|
private async void okButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (_uChecked && _user != null && !string.IsNullOrEmpty(_user.Username) && !string.IsNullOrEmpty(secretBox.Text))
|
|
{
|
|
// PERFORM ACTUAL REGISTER
|
|
Auth = new Auth
|
|
{
|
|
Username = _user.Username,
|
|
Secret = StringAssemble.CalcSha512(secretBox.Text)
|
|
};
|
|
if (await PerformRegister(_user, Auth))
|
|
{
|
|
await SaveBoy.SaveAuth(Auth);
|
|
Cancelled = false;
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show(_exist ? @"Passwortänderung hat nicht geklappt -> Frag Admin" : @"Registrieren hat nicht geklappt", @"FEHLER", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
Close();
|
|
}
|
|
|
|
private async void checkUserButton_Click(object sender, EventArgs e)
|
|
{
|
|
_user = await BackendComms.GetUser(usernameBox.Text);
|
|
if (_user == null || string.IsNullOrEmpty(_user.Username) || _user.Id == 0)
|
|
{
|
|
MessageBox.Show(@"Benutzer gibts nicht", @"FEHLER", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(_user.ImageUrl))
|
|
{
|
|
pictureBox.LoadAsync(_user.ImageUrl);
|
|
pictureBox.Visible = true;
|
|
}
|
|
|
|
_exist = _users != null && _users.Any(u => u.Username == _user.Username);
|
|
MessageBox.Show($@"Hi {_user.Username} mit ID {_user.Id}", @"OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
_uChecked = true;
|
|
}
|
|
|
|
private void usernameBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
_uChecked = false;
|
|
pictureBox.Visible = false;
|
|
checkUserButton.Enabled = !string.IsNullOrWhiteSpace(usernameBox.Text);
|
|
}
|
|
|
|
private void secretBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
var same = !string.IsNullOrWhiteSpace(secretBox.Text) && !string.IsNullOrWhiteSpace(secretBox2.Text) && secretBox.Text == secretBox2.Text;
|
|
pwcheckLabel.Text = same ? @"✅" : @"❌";
|
|
okButton.Enabled = _uChecked && same;
|
|
}
|
|
|
|
private void secretBox2_TextChanged(object sender, EventArgs e)
|
|
{
|
|
var same = !string.IsNullOrWhiteSpace(secretBox.Text) && !string.IsNullOrWhiteSpace(secretBox2.Text) && secretBox.Text == secretBox2.Text;
|
|
pwcheckLabel.Text = same ? @"✅" : @"❌";
|
|
okButton.Enabled = _uChecked && same;
|
|
}
|
|
}
|