Mise en place de l'authentification Keycloak
This commit is contained in:
@@ -4,10 +4,10 @@ using ChessCubing.App.Models;
|
||||
|
||||
namespace ChessCubing.App.Services;
|
||||
|
||||
public sealed class MatchStore(BrowserBridge browser)
|
||||
public sealed class MatchStore(BrowserBridge browser, UserSession userSession)
|
||||
{
|
||||
public const string StorageKey = "chesscubing-arena-state-v2";
|
||||
public const string WindowNameKey = "chesscubing-arena-state-v2:";
|
||||
public const string StorageKeyPrefix = "chesscubing-arena-state-v3";
|
||||
public const string WindowNameKeyPrefix = "chesscubing-arena-state-v3";
|
||||
|
||||
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
|
||||
{
|
||||
@@ -16,6 +16,8 @@ public sealed class MatchStore(BrowserBridge browser)
|
||||
|
||||
private bool _dirty;
|
||||
private long _lastPersistedAt;
|
||||
private string? _activeStorageKey;
|
||||
private string? _activeWindowNameKey;
|
||||
|
||||
public MatchState? Current { get; private set; }
|
||||
|
||||
@@ -23,6 +25,7 @@ public sealed class MatchStore(BrowserBridge browser)
|
||||
|
||||
public async Task EnsureLoadedAsync()
|
||||
{
|
||||
var storageScope = await SyncStorageScopeAsync();
|
||||
if (IsLoaded)
|
||||
{
|
||||
return;
|
||||
@@ -30,7 +33,7 @@ public sealed class MatchStore(BrowserBridge browser)
|
||||
|
||||
try
|
||||
{
|
||||
var raw = await browser.ReadMatchJsonAsync();
|
||||
var raw = await browser.ReadMatchJsonAsync(storageScope.StorageKey, storageScope.WindowNameKey);
|
||||
if (!string.IsNullOrWhiteSpace(raw))
|
||||
{
|
||||
var parsed = JsonSerializer.Deserialize<MatchState>(raw, JsonOptions);
|
||||
@@ -61,6 +64,7 @@ public sealed class MatchStore(BrowserBridge browser)
|
||||
|
||||
public async Task SaveAsync()
|
||||
{
|
||||
var storageScope = await SyncStorageScopeAsync();
|
||||
if (!IsLoaded)
|
||||
{
|
||||
return;
|
||||
@@ -68,14 +72,14 @@ public sealed class MatchStore(BrowserBridge browser)
|
||||
|
||||
if (Current is null)
|
||||
{
|
||||
await browser.ClearMatchAsync();
|
||||
await browser.ClearMatchAsync(storageScope.StorageKey, storageScope.WindowNameKey);
|
||||
_dirty = false;
|
||||
_lastPersistedAt = MatchEngine.NowUnixMs();
|
||||
return;
|
||||
}
|
||||
|
||||
var json = JsonSerializer.Serialize(Current, JsonOptions);
|
||||
await browser.WriteMatchJsonAsync(json);
|
||||
await browser.WriteMatchJsonAsync(storageScope.StorageKey, storageScope.WindowNameKey, json);
|
||||
_dirty = false;
|
||||
_lastPersistedAt = MatchEngine.NowUnixMs();
|
||||
}
|
||||
@@ -97,10 +101,27 @@ public sealed class MatchStore(BrowserBridge browser)
|
||||
|
||||
public async Task ClearAsync()
|
||||
{
|
||||
var storageScope = await SyncStorageScopeAsync();
|
||||
Current = null;
|
||||
IsLoaded = true;
|
||||
_dirty = false;
|
||||
_lastPersistedAt = MatchEngine.NowUnixMs();
|
||||
await browser.ClearMatchAsync();
|
||||
await browser.ClearMatchAsync(storageScope.StorageKey, storageScope.WindowNameKey);
|
||||
}
|
||||
|
||||
private async ValueTask<UserStorageScope> SyncStorageScopeAsync()
|
||||
{
|
||||
var storageScope = await userSession.GetStorageScopeAsync();
|
||||
if (_activeStorageKey is not null &&
|
||||
(_activeStorageKey != storageScope.StorageKey || _activeWindowNameKey != storageScope.WindowNameKey))
|
||||
{
|
||||
Current = null;
|
||||
IsLoaded = false;
|
||||
_dirty = false;
|
||||
}
|
||||
|
||||
_activeStorageKey = storageScope.StorageKey;
|
||||
_activeWindowNameKey = storageScope.WindowNameKey;
|
||||
return storageScope;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user