bib_Talk_Chatprogramm/bibtalk/bib-talk/Login.xaml.cs

104 lines
3.4 KiB
C#
Raw Normal View History

2024-06-10 15:57:04 +02:00
using Newtonsoft.Json;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows;
namespace bib_talk
{
public partial class Login : Window
{
// Field to store logged in user
public static string loggedInUser;
public Login()
{
InitializeComponent();
}
private void DraggableArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ButtonState == MouseButtonState.Pressed)
{
DragMove();
}
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
RegisterWindow register = new RegisterWindow();
register.Show();
this.Close();
}
private void WeiterButton_Click(object sender, RoutedEventArgs e)
{
if (usernbox.Text != "" && passwordbox.Password != "")
{
LoginServer();
}
}
private void passwordbox_PasswordChanged(object sender, RoutedEventArgs e)
{
if (usernbox.Text != "" && passwordbox.Password != "")
{
SolidColorBrush customBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF6332A0"));
weiterbutton.Foreground = Brushes.White;
weiterbutton.Background = customBrush;
}
else
{
SolidColorBrush customBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF323134"));
weiterbutton.Foreground = Brushes.Gray;
weiterbutton.Background = customBrush;
}
}
private void Register_Click(object sender, RoutedEventArgs e)
{
RegisterWindow register = new RegisterWindow();
register.Show();
this.Close();
}
public async Task LoginServer()
{
string username = usernbox.Text;
string password = passwordbox.Password;
var loginData = new { Username = username, Password = password, Email = "", Birthday = "", IsOnline = ""};
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();
this.Close();
}
else
{
var responseContent = await response.Content.ReadAsStringAsync();
MessageBox.Show($"Login failed: {responseContent}");
}
}
}
}
}