LANCommander/LANCommander.SDK/Providers/TokenProvider.cs
Aaron Powell 2da5aa0aa4 Fix token lost after IOptionsMonitor cache invalidation
TokenProvider.GetToken() was reading solely from settingsProvider.CurrentValue,
which rebuilds from the YAML file after configRefresher.RefreshAsync() calls
OnReload(). Since the settings file save is debounced by 1 second, the rebuilt
Settings object has a null token.

Fix: TokenProvider now keeps a local _cachedToken field that is set immediately
during SetToken() and used as the primary source in GetToken(), falling back
to the settings provider only for tokens loaded from disk (e.g., app restart
with stored credentials).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-21 15:12:57 +11:00

22 lines
No EOL
587 B
C#

using LANCommander.SDK.Abstractions;
using LANCommander.SDK.Models;
namespace LANCommander.SDK.Providers;
public class TokenProvider(ISettingsProvider settingsProvider) : ITokenProvider
{
private AuthToken? _cachedToken;
public void SetToken(AuthToken token)
{
_cachedToken = token;
settingsProvider.Update(s =>
{
s.Authentication.Token = token;
s.Authentication.OfflineModeEnabled = false;
});
}
AuthToken ITokenProvider.GetToken() => _cachedToken ?? settingsProvider.CurrentValue.Authentication?.Token;
}