Skip to content

Commit

Permalink
Fix bug requiring constant login when creating a new account.
Browse files Browse the repository at this point in the history
  • Loading branch information
aiguoli committed Dec 20, 2023
1 parent 6970e23 commit 6bb005d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
29 changes: 9 additions & 20 deletions SimpleList/Services/OneDrive.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Graph;
using Microsoft.Graph.Drives.Item.Items.Item.Restore;
using Microsoft.Graph.Models;
Expand Down Expand Up @@ -221,43 +220,33 @@ public Task<string> GetAuthorizationTokenAsync(Uri uri, Dictionary<string, objec
public AllowedHostsValidator AllowedHostsValidator { get; }
}

public async Task Login(bool silent = true)
public async Task Login()
{
TokenProvider tokenProvider = new(async Task<string> (string[] scopes) =>
{
IEnumerable<IAccount> accounts = await PublicClientApp.GetAccountsAsync().ConfigureAwait(false);

try
{
if (silent)
{
authResult = await PublicClientApp
.AcquireTokenSilent(scopes, accounts.FirstOrDefault(account => account.HomeAccountId.Identifier == HomeAccountId))
.ExecuteAsync();
if (authResult != null)
{
IsAuthenticated = true;
}
} else
{
throw new MsalUiRequiredException("404", "No cache found, please sign in again.");
}
authResult = await PublicClientApp
.AcquireTokenSilent(scopes, accounts.First(account => account.HomeAccountId.Identifier == HomeAccountId))
.ExecuteAsync();
}
catch (MsalUiRequiredException)
catch (Exception exception) when (exception is MsalUiRequiredException || exception is InvalidOperationException)
{
try
{
authResult = await PublicClientApp.AcquireTokenInteractive(scopes).ExecuteAsync();
if (authResult != null)
{
IsAuthenticated = true;
}
}
catch (MsalException msalex)
{
Console.WriteLine(msalex);
}
}
if (authResult != null)
{
IsAuthenticated = true;
}
HomeAccountId = authResult?.Account.HomeAccountId.Identifier;
return authResult?.AccessToken;
}, scopes);
Expand Down
2 changes: 1 addition & 1 deletion SimpleList/ViewModels/CreateDriveViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public CreateDriveViewModel(CloudViewModel cloud)
public async Task CreateDrive()
{
OneDrive drive = new();
await drive.Login(silent: false);
await drive.Login();
if (drive.IsAuthenticated)
{
DriveViewModel driveViewModel = new(drive, DisplayName);
Expand Down

0 comments on commit 6bb005d

Please sign in to comment.