83 lines
2.5 KiB
C#
83 lines
2.5 KiB
C#
using System;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
|
|
|
|
//jakob
|
|
namespace Pen_Paper_Main
|
|
{
|
|
public partial class Registrieren : Window
|
|
{
|
|
public Registrieren()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private async void Button_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
string user = BenutzernameInput.Text.Trim();
|
|
string pass = PasswortBox.Password;
|
|
string pass2 = PasswortBoxConfirm.Password;
|
|
|
|
// simple Validierung
|
|
if (string.IsNullOrWhiteSpace(user) || string.IsNullOrWhiteSpace(pass))
|
|
{
|
|
MessageBox.Show("Bitte Benutzername und Passwort eingeben.");
|
|
return;
|
|
}
|
|
if (pass != pass2)
|
|
{
|
|
MessageBox.Show("Passwörter stimmen nicht überein.");
|
|
return;
|
|
}
|
|
|
|
var result = await CallApi("register", user, pass);
|
|
|
|
if (result.ok)
|
|
{
|
|
MessageBox.Show("Registrierung erfolgreich");
|
|
var a = new Anmeldung();
|
|
a.Show();
|
|
this.Close();
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Fehler: " + result.msg);
|
|
}
|
|
}
|
|
|
|
private async Task<(bool ok, string msg)> CallApi(string action, string u, string p)
|
|
{
|
|
try
|
|
{
|
|
//versucht api zu callen
|
|
using var client = new HttpClient();
|
|
var json = JsonSerializer.Serialize(new { username = u, password = p });
|
|
var resp = await client.PostAsync(
|
|
|
|
//url der api und worauf es verweist
|
|
$"http://localhost/api/users.php?action={action}",
|
|
new StringContent(json, Encoding.UTF8, "application/json")
|
|
);
|
|
|
|
// antwort wird erwartet
|
|
var body = await resp.Content.ReadAsStringAsync();
|
|
//text als json umformatieren
|
|
using var doc = JsonDocument.Parse(body);
|
|
//liest ob ok true oder false ist (ist definiert in db.php)
|
|
bool ok = doc.RootElement.GetProperty("ok").GetBoolean();
|
|
string msg = doc.RootElement.GetProperty("msg").GetString() ?? "";
|
|
|
|
return (ok, msg);
|
|
}
|
|
catch
|
|
{
|
|
return (false, "invalid response");
|
|
}
|
|
}
|
|
}
|
|
}
|