2026-01-20 16:45:43 +01:00
|
|
|
using System.Security.Claims;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
2026-01-22 20:47:55 +00:00
|
|
|
using RealCV.Application.Interfaces;
|
2026-01-20 16:45:43 +01:00
|
|
|
|
2026-01-22 20:47:55 +00:00
|
|
|
namespace RealCV.Infrastructure.Services;
|
2026-01-20 16:45:43 +01:00
|
|
|
|
|
|
|
|
public sealed class UserContextService : IUserContextService
|
|
|
|
|
{
|
|
|
|
|
private readonly AuthenticationStateProvider _authenticationStateProvider;
|
|
|
|
|
|
|
|
|
|
public UserContextService(AuthenticationStateProvider authenticationStateProvider)
|
|
|
|
|
{
|
|
|
|
|
_authenticationStateProvider = authenticationStateProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Guid?> GetCurrentUserIdAsync()
|
|
|
|
|
{
|
|
|
|
|
var authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
|
|
|
|
|
var userIdClaim = authState.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(userIdClaim) || !Guid.TryParse(userIdClaim, out var userId))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return userId;
|
|
|
|
|
}
|
|
|
|
|
}
|