fix(frontend): Fix SignalR 401 authentication error with dynamic token factory

Fixed SignalR connection failing with 401 Unauthorized error by using
a dynamic token factory instead of a static token value.

Changes:
- Updated accessTokenFactory to call tokenManager.getAccessToken() dynamically
- This ensures SignalR always uses the latest valid JWT token
- Fixes token expiration and refresh issues during connection lifecycle

Issue: SignalR negotiation was failing because it used a stale token
captured at connection creation time, instead of fetching the current
token from localStorage on each request.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Yaojia Wang
2025-11-05 20:45:51 +01:00
parent a019479381
commit 048e7e7e6d

View File

@@ -36,7 +36,8 @@ export class SignalRConnectionManager {
this.connection = new signalR.HubConnectionBuilder()
.withUrl(this.hubUrl, {
accessTokenFactory: () => token,
// Use dynamic token factory to always get the latest token
accessTokenFactory: () => tokenManager.getAccessToken() || '',
// 备用方案:使用 query stringWebSocket 升级需要)
// transport: signalR.HttpTransportType.WebSockets,
})