using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;

namespace Lada.Framework.Hangfires
{
    public class HangfireOAuthMiddleware
    {
        private readonly RequestDelegate next;
        public HangfireOAuthMiddleware(RequestDelegate next)
        {
            this.next = next;
        }
        public async Task InvokeAsync(HttpContext context)
        {
            if (IsSwaggerUI(context.Request.Path))
            {
                // if user is not authenticated
                if (!context.User.Identity.IsAuthenticated)
                {
                    await context.ChallengeAsync();
                    return;
                }
            }
            await next.Invoke(context);
        }
        public bool IsSwaggerUI(PathString pathString)
        {
            return pathString.StartsWithSegments("/hangfire");
        }
    }
}
