Created login window and store tokens in plugin settings

This commit is contained in:
Pat Hartl 2023-01-05 18:37:13 -06:00
parent 2b81139247
commit 91b9328afc
5 changed files with 123 additions and 1 deletions

View file

@ -88,6 +88,10 @@
<DependentUpon>PlayniteSettingsView.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ViewModels\Authentication.cs" />
<Compile Include="Views\Authentication.xaml.cs">
<DependentUpon>Authentication.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
@ -107,6 +111,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\Authentication.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LANCommander.SDK\LANCommander.SDK.csproj">

View file

@ -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;

View file

@ -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; }
}
}

View file

@ -0,0 +1,22 @@
<UserControl x:Class="LANCommander.Playnite.Extension.Views.Authentication"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:ViewModels="clr-namespace:LANCommander.Playnite.Extension.ViewModels"
mc:Ignorable="d"
d:DesignHeight="130" d:DesignWidth="275" d:DataContext="{d:DesignInstance Type=ViewModels:Authentication, IsDesignTimeCreatable=True}">
<d:DesignerProperties.DesignStyle>
<Style TargetType="UserControl">
<Setter Property="Background" Value="White" />
</Style>
</d:DesignerProperties.DesignStyle>
<StackPanel Margin="20">
<TextBlock Text="Username"/>
<TextBox Name="Username" Text="{Binding UserName}" KeyDown="TextBox_KeyDown" />
<TextBlock Text="Password" />
<PasswordBox Name="Password" PasswordChanged="Password_PasswordChanged" KeyDown="TextBox_KeyDown" />
<Button Click="LoginButton_Click" x:Name="LoginButton">Login</Button>
</StackPanel>
</UserControl>

View file

@ -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);
}
}
}
}