Centralize admin auth with endpoint filter

This commit is contained in:
2026-02-05 17:11:17 +01:00
parent c03cee1777
commit 8176940d18
3 changed files with 34 additions and 18 deletions

View File

@@ -11,10 +11,10 @@ internal static class EndpointHelpers
{
public static async Task<Player?> GetAuthenticatedPlayer(HttpContext ctx, AppDbContext db)
{
if (ctx?.User?.Identity?.IsAuthenticated != true)
{
return null;
}
if (ctx?.User?.Identity?.IsAuthenticated != true) return null;
if (ctx.Items.TryGetValue(nameof(Player), out var cached) && cached is Player cachedPlayer)
return cachedPlayer;
var idValue = ctx.User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrWhiteSpace(idValue) || !Guid.TryParse(idValue, out var playerId))
@@ -28,7 +28,10 @@ internal static class EndpointHelpers
if (existing is null)
{
await Infrastructure.PlayerIdentityExtensions.SignOutPlayerAsync(ctx);
return null;
}
ctx.Items[nameof(Player)] = existing;
return existing;
}