From 91b9328afcd2444c6f56f2cf0c39950cbb122365 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Thu, 5 Jan 2023 18:37:13 -0600 Subject: [PATCH] Created login window and store tokens in plugin settings --- .../LANCommander.Playnite.Extension.csproj | 8 ++ .../PlayniteLibraryPlugin.cs | 3 +- .../ViewModels/Authentication.cs | 14 ++++ .../Views/Authentication.xaml | 22 ++++++ .../Views/Authentication.xaml.cs | 77 +++++++++++++++++++ 5 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 LANCommander.Playnite.Extension/ViewModels/Authentication.cs create mode 100644 LANCommander.Playnite.Extension/Views/Authentication.xaml create mode 100644 LANCommander.Playnite.Extension/Views/Authentication.xaml.cs diff --git a/LANCommander.Playnite.Extension/LANCommander.Playnite.Extension.csproj b/LANCommander.Playnite.Extension/LANCommander.Playnite.Extension.csproj index 7b4d23d..cd35c59 100644 --- a/LANCommander.Playnite.Extension/LANCommander.Playnite.Extension.csproj +++ b/LANCommander.Playnite.Extension/LANCommander.Playnite.Extension.csproj @@ -88,6 +88,10 @@ PlayniteSettingsView.xaml + + + Authentication.xaml + @@ -107,6 +111,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + diff --git a/LANCommander.Playnite.Extension/PlayniteLibraryPlugin.cs b/LANCommander.Playnite.Extension/PlayniteLibraryPlugin.cs index ad96393..084e32c 100644 --- a/LANCommander.Playnite.Extension/PlayniteLibraryPlugin.cs +++ b/LANCommander.Playnite.Extension/PlayniteLibraryPlugin.cs @@ -72,7 +72,8 @@ namespace LANCommander.Playnite.Extension window.Title = "Authenticate to LANCommander"; - window.Content = new PlayniteSettingsView(this); + window.Content = new Views.Authentication(this); + window.DataContext = new ViewModels.Authentication(); window.Owner = PlayniteApi.Dialogs.GetCurrentAppWindow(); window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner; diff --git a/LANCommander.Playnite.Extension/ViewModels/Authentication.cs b/LANCommander.Playnite.Extension/ViewModels/Authentication.cs new file mode 100644 index 0000000..d9a827b --- /dev/null +++ b/LANCommander.Playnite.Extension/ViewModels/Authentication.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LANCommander.Playnite.Extension.ViewModels +{ + internal class Authentication + { + public string UserName { get; set; } + public string Password { get; set; } + } +} diff --git a/LANCommander.Playnite.Extension/Views/Authentication.xaml b/LANCommander.Playnite.Extension/Views/Authentication.xaml new file mode 100644 index 0000000..56ca8ea --- /dev/null +++ b/LANCommander.Playnite.Extension/Views/Authentication.xaml @@ -0,0 +1,22 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/LANCommander.Playnite.Extension/Views/Authentication.xaml.cs b/LANCommander.Playnite.Extension/Views/Authentication.xaml.cs new file mode 100644 index 0000000..6e9ca28 --- /dev/null +++ b/LANCommander.Playnite.Extension/Views/Authentication.xaml.cs @@ -0,0 +1,77 @@ +using LANCommander.SDK.Models; +using Playnite.SDK; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace LANCommander.Playnite.Extension.Views +{ + public partial class Authentication : UserControl + { + private PlayniteLibraryPlugin Plugin; + private ViewModels.Authentication Context { get { return (ViewModels.Authentication)DataContext; } } + private PlayniteSettingsViewModel Settings { get; set; } + + public Authentication(PlayniteLibraryPlugin plugin) + { + Plugin = plugin; + Settings = Plugin.GetSettings(false) as PlayniteSettingsViewModel; + + InitializeComponent(); + } + + private void TextBox_KeyDown(object sender, KeyEventArgs e) + { + if (e.Key == Key.Enter || e.Key == Key.Return) + { + Authenticate(); + } + } + + private void LoginButton_Click(object sender, RoutedEventArgs e) + { + Authenticate(); + } + + private void Password_PasswordChanged(object sender, RoutedEventArgs e) + { + if (DataContext != null) + { + ((ViewModels.Authentication)DataContext).Password = ((PasswordBox)sender).Password; + } + } + + private void Authenticate() + { + try + { + var response = Plugin.LANCommander.Authenticate(Context.UserName, Context.Password); + + Settings.AccessToken = response.AccessToken; + Settings.RefreshToken = response.RefreshToken; + + // Probably unneeded, but why not be more secure? + Context.Password = String.Empty; + + Plugin.SavePluginSettings(Settings); + + Window.GetWindow(this).Close(); + } + catch (Exception ex) + { + Plugin.PlayniteApi.Dialogs.ShowErrorMessage(ex.Message); + } + } + } +}