feat: add API token to WebSocket URL for authentication

WebSocket connections can't send custom headers, so the token is passed
as a query parameter. Works with the backend change to support ?token= auth.
This commit is contained in:
rcourtman 2025-12-13 21:30:27 +00:00
parent a5893bba21
commit 4ad8235f7a

View file

@ -57,5 +57,24 @@ export function getPulseWebSocketUrl(path = '/ws'): string {
}
const protocol = origin.protocol === 'https:' ? 'wss:' : 'ws:';
return `${protocol}//${origin.host}${normalizedPath}`;
let url = `${protocol}//${origin.host}${normalizedPath}`;
// Add API token as query parameter if available (WebSocket doesn't support headers)
// Import dynamically to avoid circular dependencies
try {
const storage = typeof window !== 'undefined' ? window.sessionStorage : null;
if (storage) {
const stored = storage.getItem('pulse_auth');
if (stored) {
const parsed = JSON.parse(stored);
if (parsed?.type === 'token' && parsed.value) {
url += `?token=${encodeURIComponent(parsed.value)}`;
}
}
}
} catch {
// Ignore storage errors
}
return url;
}