Skip to content

Commit

Permalink
添加第三方账户验证
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSpring114 committed Nov 20, 2024
1 parent edaa61d commit 27d34e0
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 3 deletions.
2 changes: 2 additions & 0 deletions WonderLab/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using WonderLab.Services.Launch;
using WonderLab.Services.UI;
using WonderLab.ViewModels.Dialog;
using WonderLab.ViewModels.Dialog.Auth;
using WonderLab.ViewModels.Page;
using WonderLab.ViewModels.Page.Download;
using WonderLab.ViewModels.Page.Setting;
Expand Down Expand Up @@ -123,6 +124,7 @@ private static IHost ConfigureIoC(out IHost host) {

//Configure Dialog
builder.Services.AddTransient<OfflineAuthDialogViewMdoel>();
builder.Services.AddTransient<YggdrasilAuthDialogViewModel>();
builder.Services.AddTransient<ChooseAccountTypeDialogViewModel>();

//Configure Page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace WonderLab.Controls.Media.Transitions;

public sealed class EntrancePageTransition : IPageTransition {
public TimeSpan Duration { get; set; }
public Easing Easing { get; set; } = new CircularEaseInOut();
public Easing Easing { get; set; } = new SplineEasing(0.1, 0.9, 0.2, 1.0);

public EntrancePageTransition() { }

Expand Down
9 changes: 9 additions & 0 deletions WonderLab/Services/Account/AccountService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MinecraftLaunch.Classes.Models.Auth;
using MinecraftLaunch.Components.Authenticator;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace WonderLab.Services.Accounts;
Expand All @@ -24,4 +25,12 @@ public OfflineAccount CreateOfflineAccount(string name) {
_configService.Entries.Accounts.Add(account);
return account;
}

public IEnumerable<YggdrasilAccount> CreateYggdrasilAccounts(string email, string password, string url = "https://littleskin.cn/api/yggdrasil") {
var accounts = new YggdrasilAuthenticator(url, email, password)
.Authenticate();

_configService.Entries.Accounts.AddRange(accounts);
return accounts;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using WonderLab.Infrastructure.Models.Messaging;
using WonderLab.Services.Accounts;

namespace WonderLab.ViewModels.Dialog;
namespace WonderLab.ViewModels.Dialog.Auth;

public sealed partial class OfflineAuthDialogViewMdoel : ObservableObject {
private readonly AccountService _accountService;
Expand Down
56 changes: 56 additions & 0 deletions WonderLab/ViewModels/Dialog/Auth/YggdrasilAuthDialogViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Avalonia.Controls.Notifications;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using DialogHostAvalonia;
using System.Linq;
using WonderLab.Infrastructure.Models.Messaging;
using WonderLab.Services.Accounts;

namespace WonderLab.ViewModels.Dialog.Auth;

public sealed partial class YggdrasilAuthDialogViewModel : ObservableObject {
private readonly AccountService _accountService;

[ObservableProperty] private bool _isUseLittleSkin;

[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
private string _email;

[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
private string _password;

[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
private string _yggdrasilServerUrl;

public YggdrasilAuthDialogViewModel(AccountService accountService) {
_accountService = accountService;
}

private bool CanSave() => !string.IsNullOrEmpty(Email) && !string.IsNullOrEmpty(Password) && !string.IsNullOrEmpty(YggdrasilServerUrl);

partial void OnIsUseLittleSkinChanged(bool value) {
YggdrasilServerUrl = value ? "https://littleskin.cn/api/yggdrasil" : string.Empty;
}

[RelayCommand]
private void Close() {
DialogHost.Close("PART_DialogHost");
}

[RelayCommand(CanExecute = nameof(CanSave))]
private void Save() {
var accounts = _accountService.CreateYggdrasilAccounts(Email, Password, YggdrasilServerUrl)
.ToList();

Close();

if (accounts is { Count: 0 }) {
WeakReferenceMessenger.Default.Send(new NotificationMessage($"已存在用户名为的离线账户!", NotificationType.Warning));
return;
}
}
}
13 changes: 13 additions & 0 deletions WonderLab/ViewModels/Dialog/ChooseAccountTypeDialogViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using CommunityToolkit.Mvvm.Input;
using DialogHostAvalonia;
using System.Threading.Tasks;
using WonderLab.ViewModels.Dialog.Auth;
using WonderLab.Views.Dialog.Auth;

namespace WonderLab.ViewModels.Dialog;

Expand All @@ -22,4 +24,15 @@ private Task GoToOfflineAuth() => Dispatcher.UIThread.InvokeAsync(async () => {

await DialogHost.Show(dialog, "PART_DialogHost");
});

[RelayCommand]
private Task GoToYggdrasilAuth() => Dispatcher.UIThread.InvokeAsync(async () => {
Close();

YggdrasilAuthDialog dialog = new() {
DataContext = App.Get<YggdrasilAuthDialogViewModel>()
};

await DialogHost.Show(dialog, "PART_DialogHost");
});
}
52 changes: 52 additions & 0 deletions WonderLab/Views/Dialog/Auth/YggdrasilAuthDialog.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="WonderLab.Views.Dialog.Auth.YggdrasilAuthDialog">
<Grid Margin="16 24 16 0"
RowDefinitions="Auto, Auto, Auto">
<TextBlock Classes="Subtitle"
Text="创建新账户"/>

<StackPanel Grid.Row="1"
Spacing="8"
MinWidth="550"
Margin="0 16 0 24">
<TextBox Watermark="Yggdrasil 服务器地址(例:https://littleskin.cn/api/yggdrasil)"
IsEnabled="{Binding IsUseLittleSkin, Converter={StaticResource BooleanReverseConverter}}"
Text="{Binding YggdrasilServerUrl}"/>

<TextBox Watermark="电子邮箱"
Text="{Binding Email}"/>

<TextBox Watermark="密码"
Text="{Binding UserName}"/>

<CheckBox FontSize="14"
FontWeight="Regular"
IsChecked="{Binding IsUseLittleSkin}"
Content="使用 LittleSkin 进行验证"/>
</StackPanel>

<Border Grid.Row="2"
Padding="16"
Margin="-16 0"
CornerRadius="0 0 12 12"
BorderThickness="0 1.5 0 0"
Background="{DynamicResource ContentColor2}"
BorderBrush="{DynamicResource ContentColor3}">
<Grid HorizontalAlignment="Right"
ColumnDefinitions="Auto, Auto">
<Button Grid.Column="0"
Margin="0 0 8 0"
Content="取消"
Command="{Binding CloseCommand}"/>

<Button Grid.Column="1"
Content="保存"
Command="{Binding SaveCommand}"/>
</Grid>
</Border>
</Grid>
</UserControl>
13 changes: 13 additions & 0 deletions WonderLab/Views/Dialog/Auth/YggdrasilAuthDialog.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace WonderLab.Views.Dialog.Auth;

public partial class YggdrasilAuthDialog : UserControl
{
public YggdrasilAuthDialog()
{
InitializeComponent();
}
}
3 changes: 2 additions & 1 deletion WonderLab/Views/Dialog/ChooseAccountTypeDialog.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@

<Button Margin="8 0"
Grid.Column="1"
Content="第三方"/>
Content="第三方"
Command="{Binding GoToYggdrasilAuthCommand}"/>

<Button Grid.Column="2"
Content="微软"/>
Expand Down
1 change: 1 addition & 0 deletions WonderLab/Views/Page/Setting/AccountPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

<Panel Margin="0 8 0 0">
<StackPanel Spacing="8"
Margin="0 50"
VerticalAlignment="Center"
HorizontalAlignment="Center"
IsVisible="{Binding HasAccount, Converter={StaticResource BooleanReverseConverter}}">
Expand Down

0 comments on commit 27d34e0

Please sign in to comment.