Added backend functions

This commit is contained in:
2025-03-04 13:56:38 +01:00
parent a98709b0a1
commit 3a118a9b34
22 changed files with 578 additions and 5 deletions

View File

@@ -0,0 +1,29 @@
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);
}
}