From f6e3687ec241ea9ef9c4a1f0b12dbfb95352b557 Mon Sep 17 00:00:00 2001 From: WeeXnes Date: Tue, 21 Nov 2023 18:13:39 +0100 Subject: [PATCH] prework for implementing login --- WeeXnes/Core/Global.cs | 2 +- WeeXnes/Core/LoginLib.cs | 133 ++++++++++++++++++ WeeXnes/MainWindow.xaml | 8 ++ WeeXnes/Views/ProfileView/LoginView.xaml | 12 ++ WeeXnes/Views/ProfileView/LoginView.xaml.cs | 12 ++ WeeXnes/Views/ProfileView/ProfileView.xaml | 78 ++++++++++ WeeXnes/Views/ProfileView/ProfileView.xaml.cs | 83 +++++++++++ WeeXnes/WeeXnes.csproj | 13 +- 8 files changed, 339 insertions(+), 2 deletions(-) create mode 100644 WeeXnes/Core/LoginLib.cs create mode 100644 WeeXnes/Views/ProfileView/LoginView.xaml create mode 100644 WeeXnes/Views/ProfileView/LoginView.xaml.cs create mode 100644 WeeXnes/Views/ProfileView/ProfileView.xaml create mode 100644 WeeXnes/Views/ProfileView/ProfileView.xaml.cs diff --git a/WeeXnes/Core/Global.cs b/WeeXnes/Core/Global.cs index b4ac403..afcf1e5 100644 --- a/WeeXnes/Core/Global.cs +++ b/WeeXnes/Core/Global.cs @@ -8,7 +8,7 @@ namespace WeeXnes.Core { public class Information { - public const string Version = "4.4.6"; + public const string Version = "4.4.7"; public const string EncryptionHash = "8zf5#RdyQ]$4x4_"; public const string ApiUrl = "https://api.github.com/repos/weexnes/weexnessuite/releases/latest"; } diff --git a/WeeXnes/Core/LoginLib.cs b/WeeXnes/Core/LoginLib.cs new file mode 100644 index 0000000..2ce5816 --- /dev/null +++ b/WeeXnes/Core/LoginLib.cs @@ -0,0 +1,133 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Net.Http; +using Newtonsoft.Json; +using WeeXnes.Views.Settings; +using Wpf.Ui.Appearance; +using Wpf.Ui.Controls; + +namespace WeeXnes.Core +{ + + public class Auth + { + public BackgroundWorker _loginWorker { get; set; } + public UpdateVar _currentUserCache { get; private set; } = new UpdateVar(); + private string _token { get; set; } + private string _userDataUrl { get; set; } + private string _loginUrl { get; set; } + public string _email { get; set; } + public string _password { private get; set; } + public UpdateVar ExceptionCache { get; private set; } = new UpdateVar(); + + public Auth(string loginUrl, string userDataUrl) + { + this._currentUserCache.Value = null; + this._loginWorker = new BackgroundWorker(); + this._loginWorker.WorkerReportsProgress = true; + this._loginWorker.WorkerSupportsCancellation = true; + this._loginWorker.DoWork += (sender, args) => + { + try + { + Login(this._email, this._password); + GetUserData(); + } + catch (Exception ex) + { + this.ExceptionCache.Value = ex; + } + }; + this._loginWorker.RunWorkerCompleted += (sender, args) => + { + WeeXnes.Core.CustomConsole.WriteLine("LoginWorker complete"); + }; + this._loginUrl = loginUrl; + this._userDataUrl = userDataUrl; + } + public string Login(string email, string password) + { + if (String.IsNullOrEmpty(email)) + throw new NullReferenceException(); + if (String.IsNullOrEmpty(password)) + throw new NullReferenceException(); + + using (HttpClient client = new HttpClient()) + { + var postData = new Dictionary + { + { "email", email }, + { "password", password } + }; + + var content = new FormUrlEncodedContent(postData); + + HttpResponseMessage response = client.PostAsync(_loginUrl, content).Result; + + if (response.IsSuccessStatusCode) + { + string responseData = response.Content.ReadAsStringAsync().Result; + dynamic jsonData = Newtonsoft.Json.JsonConvert.DeserializeObject(responseData); + string token = jsonData.token; + _token = token; + return token; + //Console.WriteLine($"Token: {token}"); + } + else + { + WeeXnes.Core.CustomConsole.WriteLine("Error: " + response.StatusCode); + return null; + } + } + } + + public dynamic GetUserData(string token = null) + { + if(String.IsNullOrEmpty(token)) + if (String.IsNullOrEmpty(_token)) + { + return null; + } + else + { + token = _token; + } + // Create an instance of HttpClient + using (HttpClient client = new HttpClient()) + { + // Add the token to the request headers + client.DefaultRequestHeaders.Add("token", token); + + // Send the GET request + HttpResponseMessage response = client.GetAsync(_userDataUrl).Result; + + // Check if the request was successful (status code 200) + if (response.IsSuccessStatusCode) + { + // Read the response content as a string + string responseData = response.Content.ReadAsStringAsync().Result; + //Console.WriteLine(responseData); + // Parse the JSON data into a dynamic object + dynamic user = JsonConvert.DeserializeObject(responseData); + user = user.user; + // Now you can access the user object properties dynamically + WeeXnes.Core.CustomConsole.WriteLine("authenticated user: " + user.name); + //Console.WriteLine($"Email: {user.email}"); + // Access other properties as needed + _currentUserCache.Value = user; + return user; + } + else + { + // Handle the error, e.g., print the status code + _currentUserCache.Value = null; + + WeeXnes.Core.CustomConsole.WriteLine("Error: " + response.StatusCode); + return null; + } + } + } + + } +} \ No newline at end of file diff --git a/WeeXnes/MainWindow.xaml b/WeeXnes/MainWindow.xaml index d0d2c9f..4d88545 100644 --- a/WeeXnes/MainWindow.xaml +++ b/WeeXnes/MainWindow.xaml @@ -9,6 +9,7 @@ xmlns:settings="clr-namespace:WeeXnes.Views.Settings" xmlns:discordrpc="clr-namespace:WeeXnes.Views.DiscordRPC" xmlns:passwordGenerator="clr-namespace:WeeXnes.Views.PasswordGenerator" + xmlns:profile="clr-namespace:WeeXnes.Views.ProfileView" mc:Ignorable="d" Height="400" Width="500" @@ -90,6 +91,13 @@ Name="ButtonPwGen" PageTag="Gen" PageType="{x:Type passwordGenerator:PasswordGenView}"/> + + + + + diff --git a/WeeXnes/Views/ProfileView/LoginView.xaml.cs b/WeeXnes/Views/ProfileView/LoginView.xaml.cs new file mode 100644 index 0000000..bd316ff --- /dev/null +++ b/WeeXnes/Views/ProfileView/LoginView.xaml.cs @@ -0,0 +1,12 @@ +using System.Windows.Controls; + +namespace WeeXnes.Views.ProfileView +{ + public partial class LoginView : Page + { + public LoginView() + { + InitializeComponent(); + } + } +} \ No newline at end of file diff --git a/WeeXnes/Views/ProfileView/ProfileView.xaml b/WeeXnes/Views/ProfileView/ProfileView.xaml new file mode 100644 index 0000000..416dc91 --- /dev/null +++ b/WeeXnes/Views/ProfileView/ProfileView.xaml @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + diff --git a/WeeXnes/Views/ProfileView/ProfileView.xaml.cs b/WeeXnes/Views/ProfileView/ProfileView.xaml.cs new file mode 100644 index 0000000..a8c6f82 --- /dev/null +++ b/WeeXnes/Views/ProfileView/ProfileView.xaml.cs @@ -0,0 +1,83 @@ +using System; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media.Imaging; +using WeeXnes.Core; +using WeeXnes.Views.Settings; + +namespace WeeXnes.Views.ProfileView +{ + public partial class ProfileView : Page + { + public static Auth auth = new Auth( + "https://weexnes.dev:4000/login", + "https://weexnes.dev:4000/user" + ); + public ProfileView() + { + InitializeComponent(); + auth._email = ""; + auth._password = ""; + } + private void ProfileView_OnLoaded(object sender, RoutedEventArgs e) + { + auth.ExceptionCache.ValueChanged += LoginWorkerException; + auth._currentUserCache.ValueChanged += userCacheChanged; + + WeeXnes.Core.CustomConsole.WriteLine("Event hooks loaded"); + if (auth._currentUserCache.Value == null) + { + LoadingScreen.Visibility = Visibility.Visible; + auth._loginWorker.RunWorkerAsync(); + } + else + { + ProfileContentPanel.Visibility = Visibility.Visible; + } + } + + private void userCacheChanged() + { + if (auth._currentUserCache.Value != null) + { + this.Dispatcher.Invoke(() => + { + LoadProfileToGui(); + ProfileContentPanel.Visibility = Visibility.Visible; + LoadingScreen.Visibility = Visibility.Collapsed; + }); + } + else + { + ProfileContentPanel.Visibility = Visibility.Collapsed; + LoadingScreen.Visibility = Visibility.Visible; + } + } + + private void LoginWorkerException() + { + this.Dispatcher.Invoke(() => + { + new FluentMessageBox(auth.ExceptionCache.Value.Message).ShowDialog(); + }); + } + + private void LoadProfileToGui() + { + //Load Profile + UsernameLabel.Content = Convert.ToString(auth._currentUserCache.Value.name); + ProfileDesc.Content = Convert.ToString(auth._currentUserCache.Value.profileInfo.bio); + BannerBackground.ImageSource = + new BitmapImage(new Uri(Convert.ToString(auth._currentUserCache.Value.profileInfo.bannerUrl))); + } + + private void ProfileView_OnUnloaded(object sender, RoutedEventArgs e) + { + + auth.ExceptionCache.ValueChanged -= LoginWorkerException; + auth._currentUserCache.ValueChanged -= userCacheChanged; + + WeeXnes.Core.CustomConsole.WriteLine("Event hooks unloaded"); + } + } +} \ No newline at end of file diff --git a/WeeXnes/WeeXnes.csproj b/WeeXnes/WeeXnes.csproj index f3928d1..f1347a5 100644 --- a/WeeXnes/WeeXnes.csproj +++ b/WeeXnes/WeeXnes.csproj @@ -4,7 +4,7 @@ Debug AnyCPU - 4.4.6 + 4.4.7 {4B33CEE7-C74D-43B9-B99A-8B273D5195BC} WinExe WeeXnes @@ -41,6 +41,7 @@ ..\packages\DiscordRichPresence.1.0.175\lib\net35\DiscordRPC.dll + ..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll @@ -51,6 +52,7 @@ ..\packages\System.Drawing.Common.6.0.0\lib\net461\System.Drawing.Common.dll + @@ -71,6 +73,7 @@ + @@ -103,6 +106,12 @@ SaveToKeyManagerView.xaml + + LoginView.xaml + + + ProfileView.xaml + ChangePathsView.xaml @@ -141,6 +150,8 @@ + +