Skip to content

Commit

Permalink
More account refactoring
Browse files Browse the repository at this point in the history
dennisreimann committed Jan 25, 2025

Verified

This commit was signed with the committer’s verified signature.
1 parent 515a765 commit cbceadb
Showing 18 changed files with 46 additions and 71 deletions.
48 changes: 13 additions & 35 deletions BTCPayApp.Core/Auth/AuthStateProvider.cs
Original file line number Diff line number Diff line change
@@ -20,30 +20,26 @@ public class AuthStateProvider(
{
private bool _isInitialized;
private bool _refreshUserInfo;
private string? _currentStoreId;
private CancellationTokenSource? _pingCts;
private readonly SemaphoreSlim _semaphore = new(1, 1);
private readonly ClaimsPrincipal _unauthenticated = new(new ClaimsIdentity());

public BTCPayAccount? Account { get; private set; }
public AppUserInfo? UserInfo { get; private set; }
public string? CurrentStoreId { get; private set; }
public AsyncEventHandler<BTCPayAccount?>? OnBeforeAccountChange { get; set; }
public AsyncEventHandler<BTCPayAccount?>? OnAfterAccountChange { get; set; }
public AsyncEventHandler<AppUserStoreInfo?>? OnBeforeStoreChange { get; set; }
public AsyncEventHandler<AppUserStoreInfo?>? OnAfterStoreChange { get; set; }
public AsyncEventHandler<AppUserInfo?>? OnUserInfoChange { get; set; }
public AppUserStoreInfo? CurrentStore => string.IsNullOrEmpty(_currentStoreId) ? null : GetUserStore(_currentStoreId);
public AsyncEventHandler<AppUserStoreInfo?>? OnStoreChanged { get; set; }
public AsyncEventHandler<AppUserInfo?>? OnUserInfoChanged { get; set; }

public Task StartAsync(CancellationToken cancellationToken)
{
_pingCts = new CancellationTokenSource();
//syncService.LocalUpdated += SyncServiceLocalUpdated;
_ = PingOccasionally(_pingCts.Token);
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
//syncService.LocalUpdated -= SyncServiceLocalUpdated;
_pingCts?.Cancel();
return Task.CompletedTask;
}
@@ -57,15 +53,6 @@ private async Task PingOccasionally(CancellationToken pingCtsToken)
}
}

/*private Task SyncServiceLocalUpdated(object? sender, string[] keys)
{
if (keys.Contains(BTCPayAppConfig.Key))
{
// TODO: Implement this method
}
return Task.CompletedTask;
}*/

public BTCPayAppClient GetClient(string? baseUri = null)
{
if (string.IsNullOrEmpty(baseUri) && string.IsNullOrEmpty(Account?.BaseUri))
@@ -85,7 +72,7 @@ public override async Task<AuthenticationState> GetAuthenticationStateAsync()
if (!_isInitialized && Account == null)
{
Account = await configProvider.Get<BTCPayAccount>(BTCPayAccount.Key);
CurrentStoreId = (await configProvider.Get<BTCPayAppConfig>(BTCPayAppConfig.Key))?.CurrentStoreId;
_currentStoreId = (await configProvider.Get<BTCPayAppConfig>(BTCPayAppConfig.Key))?.CurrentStoreId;
_isInitialized = true;
}

@@ -120,7 +107,7 @@ public override async Task<AuthenticationState> GetAuthenticationStateAsync()

if (Account != null && UserInfo != null)
{
OnUserInfoChange?.Invoke(this, UserInfo);
OnUserInfoChanged?.Invoke(this, UserInfo);
await UpdateAccount(Account);
}

@@ -159,7 +146,7 @@ public async Task<FormResult> SetCurrentStoreId(string? storeId)
var store = GetUserStore(storeId);
if (store == null) return new FormResult(false, $"Store with ID '{storeId}' does not exist or belong to the user.");

if (store.Id != GetCurrentStore()?.Id)
if (store.Id != CurrentStore?.Id)
await SetCurrentStore(store);
}
else
@@ -171,18 +158,16 @@ public async Task<FormResult> SetCurrentStoreId(string? storeId)

private async Task SetCurrentStore(AppUserStoreInfo? store)
{
OnBeforeStoreChange?.Invoke(this, GetCurrentStore());

if (store != null)
store = await EnsureStorePos(store);

CurrentStoreId = store?.Id;
_currentStoreId = store?.Id;

var appConfig = await configProvider.Get<BTCPayAppConfig>(BTCPayAppConfig.Key) ?? new BTCPayAppConfig();
appConfig.CurrentStoreId = CurrentStoreId;
appConfig.CurrentStoreId = _currentStoreId;
await configProvider.Set(BTCPayAppConfig.Key, appConfig, true);

OnAfterStoreChange?.Invoke(this, store);
OnStoreChanged?.Invoke(this, store);
}

public async Task<AppUserStoreInfo> EnsureStorePos(AppUserStoreInfo store, bool? forceCreate = false)
@@ -209,11 +194,6 @@ public async Task<AppUserStoreInfo> EnsureStorePos(AppUserStoreInfo store, bool?
return UserInfo?.Stores?.FirstOrDefault(store => store.Id == storeId);
}

public AppUserStoreInfo? GetCurrentStore()
{
return string.IsNullOrEmpty(CurrentStoreId) ? null : GetUserStore(CurrentStoreId);
}

public async Task<FormResult<AcceptInviteResult>> AcceptInvite(string inviteUrl, CancellationToken? cancellation = default)
{
var urlParts = inviteUrl.Split("/invite/");
@@ -380,7 +360,7 @@ public async Task<FormResult<ApplicationUserData>> ChangeAccountInfo(string emai
if (UserInfo != null)
{
UserInfo.SetInfo(userData.Email!, userData.Name, userData.ImageUrl);
OnUserInfoChange?.Invoke(this, UserInfo);
OnUserInfoChanged?.Invoke(this, UserInfo);
}
return new FormResult<ApplicationUserData>(true, "Your account info has been changed.", userData);
}
@@ -404,16 +384,14 @@ private async Task UpdateAccount(BTCPayAccount account)

private async Task SetAccount(BTCPayAccount account)
{
OnBeforeAccountChange?.Invoke(this, Account);
await UpdateAccount(account);
Account = account;
UserInfo = null;
OnUserInfoChange?.Invoke(this, UserInfo);
OnUserInfoChanged?.Invoke(this, UserInfo);

NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
OnAfterAccountChange?.Invoke(this, Account);

var store = GetCurrentStore();
var store = CurrentStore;
if (store != null) await SetCurrentStore(store);
}
}
9 changes: 3 additions & 6 deletions BTCPayApp.Core/Auth/IAccountManager.cs
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ public interface IAccountManager
{
public BTCPayAccount? Account { get; }
public AppUserInfo? UserInfo { get; }
public AppUserStoreInfo? CurrentStore { get; }
public BTCPayAppClient GetClient(string? baseUri = null);
public Task<bool> CheckAuthenticated(bool refreshUser = false);
public Task<bool> IsAuthorized(string policy, object? resource = null);
@@ -19,12 +20,8 @@ public interface IAccountManager
public Task<FormResult<ApplicationUserData>> ChangePassword(string currentPassword, string newPassword, CancellationToken? cancellation = default);
public Task<FormResult<ApplicationUserData>> ChangeAccountInfo(string email, string? name, string? imageUrl, CancellationToken? cancellation = default);
public Task<FormResult> SetCurrentStoreId(string? storeId);
public AppUserStoreInfo? GetCurrentStore();
public Task<AppUserStoreInfo> EnsureStorePos(AppUserStoreInfo store, bool? forceCreate = false);
public Task Logout();
public AsyncEventHandler<BTCPayAccount?>? OnBeforeAccountChange { get; set; }
public AsyncEventHandler<BTCPayAccount?>? OnAfterAccountChange { get; set; }
public AsyncEventHandler<AppUserInfo?>? OnUserInfoChange { get; set; }
public AsyncEventHandler<AppUserStoreInfo?>? OnBeforeStoreChange { get; set; }
public AsyncEventHandler<AppUserStoreInfo?>? OnAfterStoreChange { get; set; }
public AsyncEventHandler<AppUserInfo?>? OnUserInfoChanged { get; set; }
public AsyncEventHandler<AppUserStoreInfo?>? OnStoreChanged { get; set; }
}
4 changes: 2 additions & 2 deletions BTCPayApp.Core/Helpers/StoreHelpers.cs
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ public static class StoreHelpers
public static async Task<(GenericPaymentMethodData? onchain, GenericPaymentMethodData? lightning)>
GetCurrentStorePaymentMethods(this IAccountManager accountManager)
{
var storeId = accountManager.GetCurrentStore()?.Id;
var storeId = accountManager.CurrentStore?.Id;
var pms = await accountManager.GetClient().GetStorePaymentMethods(storeId, includeConfig: true);
var onchain = pms.FirstOrDefault(pm => pm.PaymentMethodId == OnChainWalletManager.PaymentMethodId);
var lightning = pms.FirstOrDefault(pm => pm.PaymentMethodId == LightningNodeManager.PaymentMethodId);
@@ -25,7 +25,7 @@ public static class StoreHelpers
this IAccountManager accountManager,
OnChainWalletManager onChainWalletManager, LightningNodeManager lightningNodeService, bool applyOnchain, bool applyLighting)
{
var storeId = accountManager.GetCurrentStore()?.Id;
var storeId = accountManager.CurrentStore?.Id;
var userId = accountManager.UserInfo?.UserId;
var config = await onChainWalletManager.GetConfig();
if (// are user and store present?
4 changes: 2 additions & 2 deletions BTCPayApp.Tests/CoreTests.cs
Original file line number Diff line number Diff line change
@@ -161,11 +161,11 @@ await TestUtils.EventuallyAsync(async () =>
Assert.Equal(Money.Coins(2).Satoshi, utxos.Sum(coin => (Money)coin.Amount));
});

Assert.Null(node2.AccountManager.GetCurrentStore());
Assert.Null(node2.AccountManager.CurrentStore);
var store = await node2.AccountManager.GetClient().CreateStore(new CreateStoreRequest { Name = "Store1" });
Assert.True(await node2.AccountManager.CheckAuthenticated(true));
Assert.True((await node2.AccountManager.SetCurrentStoreId(store.Id)).Succeeded);
Assert.Equal(store.Id, node2.AccountManager.GetCurrentStore()?.Id);
Assert.Equal(store.Id, node2.AccountManager.CurrentStore?.Id);

var res = await node2.AccountManager.TryApplyingAppPaymentMethodsToCurrentStore(node2.OnChainWalletManager,
node2.LNManager, true, true);
2 changes: 1 addition & 1 deletion BTCPayApp.UI/App.razor
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@
}

// store
var store = AccountManager.GetCurrentStore();
var store = AccountManager.CurrentStore;
if (store != null)
{
Dispatcher.Dispatch(new StoreState.SetStoreInfo(store));
2 changes: 1 addition & 1 deletion BTCPayApp.UI/Components/GettingStarted.razor
Original file line number Diff line number Diff line change
@@ -87,7 +87,7 @@

@code {
private SetupStatus? _setupStatus;
private string? StoreId => AccountManager.GetCurrentStore()!.Id;
private string? StoreId => AccountManager.CurrentStore!.Id;
private SetupState SetupStateConnection => _setupStatus?.SetupStateConnection() ?? SetupState.Undetermined;
private SetupState SetupStateAccount => _setupStatus?.SetupStateAccount() ?? SetupState.Undetermined;
private SetupState SetupStateOnchain => _setupStatus?.SetupStateOnchain() ?? SetupState.Undetermined;
2 changes: 1 addition & 1 deletion BTCPayApp.UI/Components/Layout/NavbarBottom.razor
Original file line number Diff line number Diff line change
@@ -70,5 +70,5 @@
}

private bool HasStore => StoreId != null;
private string? StoreId => AccountManager.GetCurrentStore()?.Id;
private string? StoreId => AccountManager.CurrentStore?.Id;
}
2 changes: 1 addition & 1 deletion BTCPayApp.UI/Components/SetupStatus.razor
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@
}

public SetupState SetupStateAccount() {
return string.IsNullOrEmpty(AccountManager.GetCurrentStore()?.Id) ? SetupState.Pending : SetupState.Completed;
return string.IsNullOrEmpty(AccountManager.CurrentStore?.Id) ? SetupState.Pending : SetupState.Completed;
}

public SetupState SetupStateOnchain() {
2 changes: 1 addition & 1 deletion BTCPayApp.UI/Pages/InvoicePage.razor
Original file line number Diff line number Diff line change
@@ -330,7 +330,7 @@
}
}

private string? StoreId => AccountManager.GetCurrentStore()?.Id;
private string? StoreId => AccountManager.CurrentStore?.Id;
private AppUserStoreInfo? StoreInfo => StoreState.Value.StoreInfo;
private InvoiceData? Invoice => !string.IsNullOrEmpty(InvoiceId) ? StoreState.Value.GetInvoice(InvoiceId!)?.Data : null;
private bool Loading => !string.IsNullOrEmpty(InvoiceId) && StoreState.Value.GetInvoice(InvoiceId!)?.Loading is true;
2 changes: 1 addition & 1 deletion BTCPayApp.UI/Pages/InvoicesPage.razor
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@
</section>

@code {
private string? StoreId => AccountManager.GetCurrentStore()?.Id;
private string? StoreId => AccountManager.CurrentStore?.Id;
private AppUserStoreInfo? StoreInfo => StoreState.Value.StoreInfo;
private IEnumerable<InvoiceData>? Invoices => StoreState.Value.Invoices?.Data;
private bool Loading => StoreState.Value.Invoices?.Loading is true;
2 changes: 1 addition & 1 deletion BTCPayApp.UI/Pages/Lightning/SettingsPage.razor
Original file line number Diff line number Diff line change
@@ -128,7 +128,7 @@
@code {
private LightningConfig? _config;
private LDKNode? Node => LightningNodeManager.Node;
private string? StoreId => AccountManager.GetCurrentStore()?.Id;
private string? StoreId => AccountManager.CurrentStore?.Id;
private static string PaymentMethodId => LightningNodeManager.PaymentMethodId;
private string? _storePaymentMethodIdentifier;
// We need a ui/component for the api keys now.
2 changes: 1 addition & 1 deletion BTCPayApp.UI/Pages/PointOfSalePage.razor
Original file line number Diff line number Diff line change
@@ -118,7 +118,7 @@ else
}

private string? _errorMessage;
private AppUserStoreInfo? Store => AccountManager.GetCurrentStore();
private AppUserStoreInfo? Store => AccountManager.CurrentStore;
private string? StoreId => Store?.Id;
private string? AppId => Store?.PosAppId ?? AppData?.Id;
private PointOfSaleAppData? AppData => StoreState.Value.PointOfSale?.Data;
2 changes: 1 addition & 1 deletion BTCPayApp.UI/Pages/Settings/IndexPage.razor
Original file line number Diff line number Diff line change
@@ -286,7 +286,7 @@
private bool? _biometricAuthAvailable;
private SettingsModel Model { get; set; } = new();
private bool HasPasscode => !string.IsNullOrEmpty(_config?.Passcode);
private AppUserStoreInfo? CurrentStore => AccountManager.GetCurrentStore();
private AppUserStoreInfo? CurrentStore => AccountManager.CurrentStore;
private AppInstanceInfo? InstanceInfo => UiState.Value.Instance?.Data;
private AppUserInfo? CurrentUser => UserState.Value.Info?.Data;
private IEnumerable<StoreUserData>? StoreUsers => StoreState.Value.Users?.Data;
2 changes: 1 addition & 1 deletion BTCPayApp.UI/Pages/Settings/PosPage.razor
Original file line number Diff line number Diff line change
@@ -244,7 +244,7 @@
public AppItem[]? Items { get; set; }
}

private string StoreId => AccountManager.GetCurrentStore()!.Id;
private string StoreId => AccountManager.CurrentStore!.Id;
private PointOfSaleAppData? AppData => StoreState.Value.PointOfSale?.Data;
private bool Loading => StoreState.Value.PointOfSale?.Loading is true;
private bool Sending => StoreState.Value.PointOfSale?.Sending is true;
2 changes: 1 addition & 1 deletion BTCPayApp.UI/Pages/Settings/SelectStorePage.razor
Original file line number Diff line number Diff line change
@@ -94,7 +94,7 @@
{
await base.OnInitializedAsync();

_initialStoreId = Model.StoreId = AccountManager.GetCurrentStore()?.Id;
_initialStoreId = Model.StoreId = AccountManager.CurrentStore?.Id;

ActionSubscriber.SubscribeToAction<UserState.SetInfo>(this, action =>
{
2 changes: 1 addition & 1 deletion BTCPayApp.UI/Pages/SignedOut/SignedOutBasePage.razor
Original file line number Diff line number Diff line change
@@ -167,7 +167,7 @@
var route = AccountManager.Account != null ? Routes.Connect : Routes.Welcome;
if (authState.User.Identity?.IsAuthenticated is true)
{
var storeId = AccountManager.GetCurrentStore()?.Id;
var storeId = AccountManager.CurrentStore?.Id;
switch (ConnectionManager.ConnectionState)
{
case BTCPayConnectionState.Connecting or BTCPayConnectionState.Syncing:
2 changes: 1 addition & 1 deletion BTCPayApp.UI/Pages/Wallet/SettingsPage.razor
Original file line number Diff line number Diff line change
@@ -106,7 +106,7 @@

@code {
private string PaymentMethodId => OnChainWalletManager.PaymentMethodId;
private string? StoreId => AccountManager.GetCurrentStore()?.Id;
private string? StoreId => AccountManager.CurrentStore?.Id;
private string? _storePaymentMethodIdentifier;
private string? _errorMessage;
private string? _successMessage;
Loading

0 comments on commit cbceadb

Please sign in to comment.