6a0c97dd9e
Add Authorization Bearer header to all fetch calls (apps, stream start). Handle 401 responses by clearing token and redirecting to login. Pass stream_token from the stream start response through to the WebTransport URL as a query parameter for proxy authentication. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
import { getToken, handleUnauthorized } from './stores/authStore.svelte';
|
|
|
|
type StreamData = {
|
|
Url: string,
|
|
CertHash: Array<number>,
|
|
Width: number,
|
|
Height: number,
|
|
StreamToken: string,
|
|
}
|
|
|
|
export async function getStreamData(appId: number, server_name: string): Promise<StreamData> {
|
|
try {
|
|
// Create the POST request payload
|
|
const width = 1920;
|
|
const height = 1080;
|
|
|
|
const payload = {
|
|
id: appId,
|
|
server: server_name,
|
|
server_mode: {
|
|
fps: 60,
|
|
width: width,
|
|
height: height,
|
|
},
|
|
stream_config: {
|
|
bitrate_kbps: 1024 * 10 * 5,
|
|
mode: {
|
|
fps: 60,
|
|
width: width,
|
|
height: height,
|
|
}
|
|
}
|
|
};
|
|
|
|
// Make POST request to start stream
|
|
const response = await fetch('/api/stream/start', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${getToken()}`,
|
|
},
|
|
body: JSON.stringify(payload)
|
|
});
|
|
|
|
if (response.status === 401) {
|
|
handleUnauthorized();
|
|
throw new Error('Unauthorized');
|
|
}
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! status: ${response.status}: ${await response.text()}`);
|
|
}
|
|
|
|
const streamDataResp = await response.json();
|
|
console.log('Stream started:', streamDataResp);
|
|
|
|
let streamData: StreamData = {
|
|
Url: streamDataResp.url,
|
|
CertHash: streamDataResp.cert_hash,
|
|
Width: width,
|
|
Height: height,
|
|
StreamToken: streamDataResp.stream_token,
|
|
};
|
|
return streamData;
|
|
|
|
} catch (error) {
|
|
console.error('Error getting stream data: ', error);
|
|
throw new Error('Failed to start stream: ' + error);
|
|
}
|
|
}
|