using Atomx.Admin.Client.Utils;
using Microsoft.AspNetCore.Authentication;

namespace Atomx.Admin.Utils
{
    public class ServerTokenProvider : ITokenProvider
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        public ServerTokenProvider(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        public async Task<string?> GetTokenAsync()
        {
            // 在Server端从HttpContext获取token
            return await _httpContextAccessor.HttpContext?.GetTokenAsync("access_token");
        }

        public async Task<bool> IsTokenValidAsync()
        {
            var token = await GetTokenAsync();
            return !string.IsNullOrEmpty(token);
        }
    }
}
