From 42ca0774164de4887fba0d92dc2fc7c2b5f9a2d7 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Mon, 22 Jun 2026 21:08:48 -0500 Subject: [PATCH] Add auto redirect to provider to launcher --- .../ViewModels/LoginViewModel.cs | 15 +++++++++++++++ .../ViewModels/MainWindowViewModel.cs | 7 ++++++- .../Clients/AuthenticationClient.cs | 18 ++++++++++++++++++ .../Models/Settings/AuthenticationSettings.cs | 1 + .../Endpoints/SettingsEndpoints.cs | 1 + 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/LANCommander.Launcher/ViewModels/LoginViewModel.cs b/LANCommander.Launcher/ViewModels/LoginViewModel.cs index a912429f..c5714818 100644 --- a/LANCommander.Launcher/ViewModels/LoginViewModel.cs +++ b/LANCommander.Launcher/ViewModels/LoginViewModel.cs @@ -52,6 +52,9 @@ public partial class LoginViewModel : ViewModelBase [ObservableProperty] private bool _isServerOffline; + [ObservableProperty] + private bool _autoRedirectToProvider; + public ObservableCollection AuthenticationProviders { get; } = new(); public event EventHandler? LoginSucceeded; @@ -94,6 +97,18 @@ public partial class LoginViewModel : ViewModelBase if (!AllowRegistration && IsRegistering) IsRegistering = false; + + AutoRedirectToProvider = await _authenticationClient.GetAutoRedirectToProviderAsync(); + } + + public async Task TryAutoRedirectToProviderAsync() + { + if (IsServerOffline || !AutoRedirectToProvider || IsLoading) + return; + + // Mirror server behavior: only auto-challenge when exactly one provider exists. + if (AuthenticationProviders.Count == 1) + await LoginWithProviderAsync(AuthenticationProviders[0]); } [RelayCommand] diff --git a/LANCommander.Launcher/ViewModels/MainWindowViewModel.cs b/LANCommander.Launcher/ViewModels/MainWindowViewModel.cs index b729a9cc..ae7feae6 100644 --- a/LANCommander.Launcher/ViewModels/MainWindowViewModel.cs +++ b/LANCommander.Launcher/ViewModels/MainWindowViewModel.cs @@ -181,8 +181,12 @@ public partial class MainWindowViewModel : ViewModelBase if (serverOnline) await LoginViewModel.LoadAuthenticationProvidersAsync(); - + CurrentView = LoginViewModel; + + if (serverOnline) + await LoginViewModel.TryAutoRedirectToProviderAsync(); + return; } @@ -196,6 +200,7 @@ public partial class MainWindowViewModel : ViewModelBase LoginViewModel.IsServerOffline = false; await LoginViewModel.LoadAuthenticationProvidersAsync(); CurrentView = LoginViewModel; + await LoginViewModel.TryAutoRedirectToProviderAsync(); } private async void OnLoginSucceeded(object? sender, EventArgs e) diff --git a/LANCommander.SDK/Clients/AuthenticationClient.cs b/LANCommander.SDK/Clients/AuthenticationClient.cs index 1b64f8b1..cda1c337 100644 --- a/LANCommander.SDK/Clients/AuthenticationClient.cs +++ b/LANCommander.SDK/Clients/AuthenticationClient.cs @@ -210,6 +210,24 @@ public class AuthenticationClient( } } + public async Task GetAutoRedirectToProviderAsync() + { + try + { + var settings = await apiRequestFactory + .Create() + .UseRoute("/api/Settings") + .GetAsync(); + + return settings?.Authentication?.AutoRedirectToProvider ?? false; + } + catch + { + // Older servers don't expose this setting - default to no auto-redirect + return false; + } + } + public Uri GetAuthenticationProviderLoginUrl(string provider) { return connectionClient.GetServerAddress().Join($"api/Auth/Login?Provider={provider}"); diff --git a/LANCommander.SDK/Models/Settings/AuthenticationSettings.cs b/LANCommander.SDK/Models/Settings/AuthenticationSettings.cs index def93f28..3ad8300e 100644 --- a/LANCommander.SDK/Models/Settings/AuthenticationSettings.cs +++ b/LANCommander.SDK/Models/Settings/AuthenticationSettings.cs @@ -11,4 +11,5 @@ public class AuthenticationSettings public AuthToken Token { get; set; } public bool OfflineModeEnabled { get; set; } public bool AllowRegistration { get; set; } = true; + public bool AutoRedirectToProvider { get; set; } = false; } \ No newline at end of file diff --git a/LANCommander.Server/Endpoints/SettingsEndpoints.cs b/LANCommander.Server/Endpoints/SettingsEndpoints.cs index 1b01d7ea..cc21aede 100644 --- a/LANCommander.Server/Endpoints/SettingsEndpoints.cs +++ b/LANCommander.Server/Endpoints/SettingsEndpoints.cs @@ -32,6 +32,7 @@ public static class SettingsEndpoints Authentication = new { AllowRegistration = settingsProvider.CurrentValue.Server.Authentication.AllowRegistration, + AutoRedirectToProvider = settingsProvider.CurrentValue.Server.Authentication.AutoRedirectToProvider, } };