39 lines
1.3 KiB
C#
39 lines
1.3 KiB
C#
using System.IdentityModel.Tokens.Jwt;
|
|
using ExpenseTracker.Application.Common.Interfaces.Services;
|
|
using ExpenseTracker.Application.Common.Models;
|
|
|
|
namespace ExpenseTracker.Api.Services;
|
|
|
|
public class SessionUserService : ISessionUserService
|
|
{
|
|
private readonly HttpContext _httpContext;
|
|
|
|
public SessionUserService(IHttpContextAccessor httpContextAccessor)
|
|
{
|
|
_httpContext = httpContextAccessor.HttpContext!;
|
|
}
|
|
|
|
public string? Id => _httpContext.User.Claims
|
|
.FirstOrDefault(c => c.Properties
|
|
.Any(p => p.Value == JwtRegisteredClaimNames.Sub))
|
|
?.Value;
|
|
|
|
public string? Email => _httpContext.User.Claims
|
|
.FirstOrDefault(c => c.Properties
|
|
.Any(p => p.Value == JwtRegisteredClaimNames.Email))
|
|
?.Value;
|
|
|
|
public ICollection<string> Roles => _httpContext.User.Claims
|
|
.Where(c => c.Properties
|
|
.Any(p => p.Value == "roles"))
|
|
.Select(c => c.Value)
|
|
.ToArray();
|
|
|
|
public bool IsAdministrator => Roles.Contains(IdentityRoles.Administrator.ToString());
|
|
|
|
public bool IsAuthenticated => Id != null;
|
|
|
|
public string? AccessToken => _httpContext.Request.Cookies["accessToken"];
|
|
public string? RefreshToken => _httpContext.Request.Cookies["refreshToken"];
|
|
}
|