0c8657713b
Move DTOs from ApiContracts to appropriate locations: - SignalR DTOs → ViewModels (renamed Dto→ViewModel suffix) - Pipeline DTOs → Models/Pipelines - UserInfoDto → Models/Auth - DataUpdateDto → Models/Infrastructure
54 lines
1.5 KiB
C#
54 lines
1.5 KiB
C#
using System.Text.Json;
|
|
using JdeScoping.Core.Models.Auth;
|
|
using Microsoft.JSInterop;
|
|
|
|
namespace JdeScoping.Client.Auth;
|
|
|
|
/// <summary>
|
|
/// Stores user info in browser sessionStorage via JS interop.
|
|
/// Uses sessionStorage (not localStorage) so it clears on browser close,
|
|
/// matching cookie session behavior.
|
|
/// </summary>
|
|
public class UserStorageService : IUserStorageService
|
|
{
|
|
private const string UserKey = "jdescoping_user";
|
|
private readonly IJSRuntime _jsRuntime;
|
|
|
|
public UserStorageService(IJSRuntime jsRuntime)
|
|
{
|
|
_jsRuntime = jsRuntime;
|
|
}
|
|
|
|
public async Task<UserInfoDto?> GetUserAsync()
|
|
{
|
|
try
|
|
{
|
|
var json = await _jsRuntime.InvokeAsync<string?>("jdeScopingInterop.getSessionStorage", UserKey);
|
|
if (string.IsNullOrEmpty(json))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return JsonSerializer.Deserialize<UserInfoDto>(json, new JsonSerializerOptions
|
|
{
|
|
PropertyNameCaseInsensitive = true
|
|
});
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public async Task SetUserAsync(UserInfoDto user)
|
|
{
|
|
var json = JsonSerializer.Serialize(user);
|
|
await _jsRuntime.InvokeVoidAsync("jdeScopingInterop.setSessionStorage", UserKey, json);
|
|
}
|
|
|
|
public async Task RemoveUserAsync()
|
|
{
|
|
await _jsRuntime.InvokeVoidAsync("jdeScopingInterop.removeSessionStorage", UserKey);
|
|
}
|
|
}
|