Skip to content

Commit

Permalink
chore: Minor login fixes and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
morning4coffe-dev committed Apr 4, 2024
1 parent 756f903 commit 45e5e20
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 42 deletions.
29 changes: 11 additions & 18 deletions ProjectSBS/ProjectSBS/Business/ViewModels/LoginViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Windows.System;
using UserModel = ProjectSBS.Business.Models;

namespace ProjectSBS.Business.ViewModels;

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

Expand All @@ -37,7 +33,6 @@ private async Task Login()
try
{
success = await _userService.AuthenticateAsync();

}
catch (Exception ex)
{
Expand All @@ -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<ISettingsService>().ContinueWithoutLogin = false;
SendAnalytics(true);
}

Expand All @@ -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<ISettingsService>().ContinueWithoutLogin = true;
_navigation.Navigate(typeof(MainPage));
Expand All @@ -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"));
}
14 changes: 0 additions & 14 deletions ProjectSBS/ProjectSBS/Business/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
11 changes: 5 additions & 6 deletions ProjectSBS/ProjectSBS/Business/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -74,7 +73,6 @@ public SettingsViewModel(
IUserService userService
)
{
_localizer = localizer;
_userService = userService;
_currencyCache = currencyCache;
_itemService = itemService;
Expand Down Expand Up @@ -113,6 +111,9 @@ public override async void Load()
SelectedCurrency = _settingsService.DefaultCurrency;
NotificationTime = _settingsService.NotificationTime;
}
public override void Unload()
{
}

[RelayCommand]
public async Task LaunchNotificationSettings() =>
Expand All @@ -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() =>
Expand All @@ -133,8 +131,9 @@ private void Login() =>
[RelayCommand]
private void Logout()
{
_userService.Logout();
_navigation.Navigate(typeof(LoginPage));

_userService.LogoutAsync();
}

[RelayCommand]
Expand Down
2 changes: 1 addition & 1 deletion ProjectSBS/ProjectSBS/Services/User/IUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public interface IUserService
Task<bool> UploadData(string content, string relativeLocalPath, CancellationToken token = default);
Task<Stream?> RetrieveData(string relativeLocalPath, CancellationToken token);

void Logout();
Task LogoutAsync();
}
12 changes: 9 additions & 3 deletions ProjectSBS/ProjectSBS/Services/User/MsalUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ private void Initialize()
requestConfiguration.QueryParameters.Select =
["displayName", "mail"];
});

var fullName = user?.DisplayName;
var mail = user?.Mail;

Expand Down Expand Up @@ -184,16 +183,23 @@ public async Task<bool> 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<ISettingsService>().ContinueWithoutLogin = false;

OnLoggedInChanged?.Invoke(this, _currentUser);
}


[MemberNotNull(nameof(_app))]
private async Task EnsureIdentityClientAsync()
{
Expand Down

0 comments on commit 45e5e20

Please sign in to comment.