using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace TakeoutSaaS.TenantApi.Auth;

/// <summary>
/// SignalR WebSocket 连接的 JWT 认证事件处理。
/// </summary>
public static class SignalRJwtEvents
{
    /// <summary>
    /// 从 query string 提取 access_token 供 SignalR Hub 认证使用。
    /// </summary>
    public static Task OnMessageReceived(MessageReceivedContext context)
    {
        // 1. 仅对 Hub 路径生效
        if (context.Request.Path.StartsWithSegments("/hubs"))
        {
            var token = context.Request.Query["access_token"];
            if (!string.IsNullOrEmpty(token))
            {
                context.Token = token;
            }
        }

        return Task.CompletedTask;
    }
}
