using OED.Api.Core.Interfaces.Services; namespace OED.Api.Infrastructure.Auth; // Runs on every request — reads the session cookie and stamps CharacterId onto HttpContext.Items public class SessionMiddleware(RequestDelegate next) { public async Task InvokeAsync(HttpContext context, ISessionService sessionService) { if (context.Request.Cookies.TryGetValue("session", out var sessionId)) { var session = await sessionService.GetAsync(sessionId); if (session is not null) { context.Items["CharacterId"] = session.CharacterId; context.Items["CharacterName"] = session.CharacterName; context.Items["Session"] = session; } } await next(context); } }