Files
jdescopingtool/NEW/src/JdeScoping.Client/Auth/UserStorageService.cs
T
Joseph Doherty 0c8657713b refactor(core): reorganize DTOs into Models and ViewModels folders
Move DTOs from ApiContracts to appropriate locations:
- SignalR DTOs → ViewModels (renamed Dto→ViewModel suffix)
- Pipeline DTOs → Models/Pipelines
- UserInfoDto → Models/Auth
- DataUpdateDto → Models/Infrastructure
2026-01-19 00:34:57 -05:00

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);
}
}