29 lines
898 B
C#
29 lines
898 B
C#
using WorkTime.Shared.Services;
|
|
|
|
namespace WorkTime.Api.Services;
|
|
|
|
internal class AuthService(IHttpContextAccessor accessor) : IAuthService {
|
|
|
|
public Task<bool> IsAuthenticated() {
|
|
var header = accessor.HttpContext?.Request.Headers[IAuthService.HeaderName];
|
|
if (header is not { Count: 1 })
|
|
return Task.FromResult(false);
|
|
|
|
var value = header.Value[0]!.Replace("Bearer ", "");
|
|
if (Guid.TryParse(value, out var guid))
|
|
return Task.FromResult(false);
|
|
|
|
return Task.FromResult(guid != Guid.Empty);
|
|
}
|
|
|
|
public async Task<Guid> GetCurrentUserId() {
|
|
if (!await IsAuthenticated())
|
|
return Guid.Empty;
|
|
|
|
var header = accessor.HttpContext?.Request.Headers[IAuthService.HeaderName]!;
|
|
var value = header.Value[0]!.Replace("Bearer ", "");
|
|
|
|
return Guid.Parse(value);
|
|
}
|
|
|
|
} |