2024-07-01 16:21:03 +02:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
|
|
|
|
namespace bib_talk.Repository
|
|
|
|
|
{
|
|
|
|
|
public class LoginDatabase
|
|
|
|
|
{
|
2024-08-28 13:53:39 +02:00
|
|
|
|
Login loginWindow; // Login Window initialisieren
|
2024-07-01 16:21:03 +02:00
|
|
|
|
|
2024-08-28 13:53:39 +02:00
|
|
|
|
// Konstruker der LoginDatabase
|
2024-07-01 16:21:03 +02:00
|
|
|
|
public LoginDatabase(Login loginWindow)
|
|
|
|
|
{
|
2024-08-28 13:53:39 +02:00
|
|
|
|
this.loginWindow = loginWindow; // Login Window deklarieren
|
2024-07-01 16:21:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 13:53:39 +02:00
|
|
|
|
// Methode um den User einzuloggen via eines Server aufrufs
|
2024-07-01 16:21:03 +02:00
|
|
|
|
public async Task LoginServer()
|
|
|
|
|
{
|
|
|
|
|
string username = loginWindow.usernbox.Text;
|
|
|
|
|
string password = loginWindow.passwordbox.Password;
|
2024-07-02 22:40:43 +02:00
|
|
|
|
var loginData = new { Username = username, Password = password, Email = "", Birthday = "", IsOnline = ""};
|
2024-07-01 16:21:03 +02:00
|
|
|
|
string json = JsonConvert.SerializeObject(loginData);
|
|
|
|
|
|
|
|
|
|
using (var client = new HttpClient())
|
|
|
|
|
{
|
|
|
|
|
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
|
var response = await client.PostAsync("http://daddypig.dns.navy:5114/api/users/login", content);
|
|
|
|
|
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
var responseContent = await response.Content.ReadAsStringAsync();
|
|
|
|
|
var responseData = JsonConvert.DeserializeObject<dynamic>(responseContent);
|
|
|
|
|
string loggedInUsername = responseData.username;
|
|
|
|
|
MainWindow mainWindow = new MainWindow(loggedInUsername);
|
|
|
|
|
mainWindow.Show();
|
|
|
|
|
loginWindow.Close();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var responseContent = await response.Content.ReadAsStringAsync();
|
|
|
|
|
MessageBox.Show($"Login failed: {responseContent}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|