namespace RpgRoller.Api;

internal sealed class RequireSessionTokenFilter : IEndpointFilter
{
    public ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
    {
        if (!context.HttpContext.TryReadSessionTokenFromCookie(out var sessionToken))
            return ValueTask.FromResult<object?>(TypedResults.Unauthorized());

        context.HttpContext.StoreSessionToken(sessionToken);
        return next(context);
    }
}