using System; using System.Net.Http; using System.Text; using System.Text.Json; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace Pen_Paper_Main { public partial class Anmeldung : Window { public Anmeldung() { InitializeComponent(); } private async void Anmelden_Click(object sender, RoutedEventArgs e) { string user = BenutzernameInput.Text; string pass = PasswortBox.Password; var result = await CallApi("login", user, pass); if (result.ok) { MessageBox.Show("Anmeldung erfolgreich"); Lobby lobby = new Lobby(); lobby.Show(); this.Close(); } else { MessageBox.Show("Fehler: " + result.msg); } } private async Task<(bool ok,string msg)> CallApi(string action, string u, string p) { using var client = new HttpClient(); var json = JsonSerializer.Serialize(new { username = u, password = p }); var resp = await client.PostAsync($"http://localhost/api/users.php?action={action}", new StringContent(json, Encoding.UTF8, "application/json")); var body = await resp.Content.ReadAsStringAsync(); try { var doc = JsonDocument.Parse(body); bool ok = doc.RootElement.GetProperty("ok").GetBoolean(); string msg = doc.RootElement.GetProperty("msg").GetString(); return (ok, msg); } catch { return (false, "invalid response"); } } private void PasswortBox_PasswordChanged(object sender, RoutedEventArgs e) { PasswortPlaceholder.Visibility = string.IsNullOrEmpty(PasswortBox.Password) ? Visibility.Visible : Visibility.Collapsed; } private void BenutzernameInput_TextChanged(object sender, TextChangedEventArgs e) { BenutzernamePlaceHolder.Visibility = string.IsNullOrEmpty(BenutzernameInput.Text) ? Visibility.Visible : Visibility.Collapsed; } } }