Files
1Panel/frontend/src/utils/stream-auth.ts
T
ssongliuandCityFun d57c998047 fix: serve frontend routes with fallback (#12752)
* feat: change isProductPro logic (#12712)

* fix: tighten frontend RBAC permission guards (#12717)

* fix: tighten frontend RBAC permission guards

* feat: add permission directive coverage

* refactor frontend global store usage

* serve frontend routes with fallback

---------

Co-authored-by: CityFun <31820853+zhengkunwang223@users.noreply.github.com>
2026-05-21 12:39:49 +08:00

38 lines
1.4 KiB
Go

import { useGlobalStore } from '@/composables/useGlobalStore';
import { handleAuthResponseCode, handleAuthResponseStatus } from '@/utils/auth-response';
export const checkStreamAuth = async (url: string, currentNode?: string) => {
const { currentNode: storeCurrentNode, language } = useGlobalStore();
const controller = new AbortController();
const timeout = window.setTimeout(() => controller.abort(), 5000);
try {
const res = await fetch(url.replace(/^ws/, 'http'), {
credentials: 'include',
headers: {
'Accept-Language': language.value,
CurrentNode: encodeURIComponent(currentNode || storeCurrentNode.value),
},
signal: controller.signal,
});
const statusResult = handleAuthResponseStatus(res.status);
if (statusResult.handled) {
return statusResult.message;
}
const contentType = res.headers.get('content-type') || '';
if (!contentType.includes('application/json')) {
await res.body?.cancel();
return '';
}
const data = await res.json();
const codeResult = handleAuthResponseCode(data);
if (codeResult.handled) {
return codeResult.message;
}
return '';
} catch {
return '';
} finally {
window.clearTimeout(timeout);
}
};