diff --git a/ProjectSBS/ProjectSBS/Business/ViewModels/LoginViewModel.cs b/ProjectSBS/ProjectSBS/Business/ViewModels/LoginViewModel.cs index b0e8a99..232df87 100644 --- a/ProjectSBS/ProjectSBS/Business/ViewModels/LoginViewModel.cs +++ b/ProjectSBS/ProjectSBS/Business/ViewModels/LoginViewModel.cs @@ -1,5 +1,4 @@ using Windows.System; -using UserModel = ProjectSBS.Business.Models; namespace ProjectSBS.Business.ViewModels; @@ -10,9 +9,6 @@ public partial class LoginViewModel : ObservableObject private readonly INavigation _navigation; private readonly IItemService _itemService; - [ObservableProperty] - private UserModel.User? _user; - [ObservableProperty] private bool _indicateLoading; @@ -37,7 +33,6 @@ private async Task Login() try { success = await _userService.AuthenticateAsync(); - } catch (Exception ex) { @@ -46,20 +41,12 @@ private async Task Login() if (success) { - var user = await _userService.RetrieveUser(); - - App.Dispatcher.TryEnqueue(() => - { - User = user; - }); - + await _userService.RetrieveUser(); await _itemService.InitializeAsync(); - App.Dispatcher.TryEnqueue(() => - { - _navigation.Navigate(typeof(MainPage)); - }); + _navigation.Navigate(typeof(MainPage)); + App.Services!.GetRequiredService().ContinueWithoutLogin = false; SendAnalytics(true); } @@ -78,8 +65,14 @@ private void SendAnalytics(bool loggedIn) } [RelayCommand] - private void ContinueWithoutLogin() + private async Task ContinueWithoutLogin() { + var isLoggedIn = await _userService.AuthenticateAsync(true); + if (isLoggedIn) + { + await _userService.LogoutAsync(); + } + IndicateLoading = true; App.Services!.GetRequiredService().ContinueWithoutLogin = true; _navigation.Navigate(typeof(MainPage)); @@ -88,5 +81,5 @@ private void ContinueWithoutLogin() [RelayCommand] private async Task OpenPrivacyPolicy() => - await Launcher.LaunchUriAsync(new Uri("https://github.com/morning4coffe-dev/recurrents/blob/ebf622cb65d60c7d353af69824f63d88fa796bde/privacy-policy.md")); + await Launcher.LaunchUriAsync(new Uri("https://github.com/morning4coffe-dev/recurrents/blob/ebf622cb65d60c7d353af69824f63d88fa796bde/privacy-policy.md")); } diff --git a/ProjectSBS/ProjectSBS/Business/ViewModels/MainViewModel.cs b/ProjectSBS/ProjectSBS/Business/ViewModels/MainViewModel.cs index 79f2253..ea03917 100644 --- a/ProjectSBS/ProjectSBS/Business/ViewModels/MainViewModel.cs +++ b/ProjectSBS/ProjectSBS/Business/ViewModels/MainViewModel.cs @@ -123,21 +123,7 @@ private void GoToSettings() [RelayCommand] private void Login() { - if (IsLoggedIn) - { - //TODO There is a bug in the MenuFlyout - //MenuFlyout.ShowAttachedFlyout(UserButton); - return; - } - _navigation.Navigate(typeof(LoginPage)); _itemService.ClearItems(); } - - [RelayCommand] - private void Logout() - { - _userService.Logout(); - _navigation.Navigate(typeof(LoginPage)); - } } diff --git a/ProjectSBS/ProjectSBS/Business/ViewModels/SettingsViewModel.cs b/ProjectSBS/ProjectSBS/Business/ViewModels/SettingsViewModel.cs index 05921c0..7054833 100644 --- a/ProjectSBS/ProjectSBS/Business/ViewModels/SettingsViewModel.cs +++ b/ProjectSBS/ProjectSBS/Business/ViewModels/SettingsViewModel.cs @@ -8,7 +8,6 @@ public partial class SettingsViewModel : ViewModelBase private readonly ISettingsService _settingsService; private readonly IItemService _itemService; private readonly IUserService _userService; - private readonly IStringLocalizer _localizer; private readonly INavigation _navigation; private readonly IInteropService _interopService; @@ -74,7 +73,6 @@ public SettingsViewModel( IUserService userService ) { - _localizer = localizer; _userService = userService; _currencyCache = currencyCache; _itemService = itemService; @@ -113,6 +111,9 @@ public override async void Load() SelectedCurrency = _settingsService.DefaultCurrency; NotificationTime = _settingsService.NotificationTime; } + public override void Unload() + { + } [RelayCommand] public async Task LaunchNotificationSettings() => @@ -122,9 +123,6 @@ public async Task LaunchNotificationSettings() => public async Task LaunchLangSettings() => await WS.Launcher.LaunchUriAsync(new Uri("ms-settings:regionlanguage-adddisplaylanguage")); - public override void Unload() - { - } [RelayCommand] private void Login() => @@ -133,8 +131,9 @@ private void Login() => [RelayCommand] private void Logout() { - _userService.Logout(); _navigation.Navigate(typeof(LoginPage)); + + _userService.LogoutAsync(); } [RelayCommand] diff --git a/ProjectSBS/ProjectSBS/Services/User/IUserService.cs b/ProjectSBS/ProjectSBS/Services/User/IUserService.cs index 722d783..3ef16df 100644 --- a/ProjectSBS/ProjectSBS/Services/User/IUserService.cs +++ b/ProjectSBS/ProjectSBS/Services/User/IUserService.cs @@ -13,5 +13,5 @@ public interface IUserService Task UploadData(string content, string relativeLocalPath, CancellationToken token = default); Task RetrieveData(string relativeLocalPath, CancellationToken token); - void Logout(); + Task LogoutAsync(); } diff --git a/ProjectSBS/ProjectSBS/Services/User/MsalUser.cs b/ProjectSBS/ProjectSBS/Services/User/MsalUser.cs index 84f1109..9a1ab0e 100644 --- a/ProjectSBS/ProjectSBS/Services/User/MsalUser.cs +++ b/ProjectSBS/ProjectSBS/Services/User/MsalUser.cs @@ -116,7 +116,6 @@ private void Initialize() requestConfiguration.QueryParameters.Select = ["displayName", "mail"]; }); - var fullName = user?.DisplayName; var mail = user?.Mail; @@ -184,16 +183,23 @@ public async Task UploadData(string content, string relativeLocalPath, Can .GetAsync(cancellationToken: token); } - public void Logout() + public async Task LogoutAsync() { + _client?.Dispose(); + _client = null; _currentUser = null; - _app?.RemoveAsync(_app.GetAccountsAsync().Result.FirstOrDefault()); + + foreach (var account in await _app?.GetAccountsAsync()) + { + await _app.RemoveAsync(account); + } App.Services!.GetRequiredService().ContinueWithoutLogin = false; OnLoggedInChanged?.Invoke(this, _currentUser); } + [MemberNotNull(nameof(_app))] private async Task EnsureIdentityClientAsync() {