fix(migration): ensure auth token is retrieved from storage and cookies for requests and SSE
This commit is contained in:
parent
b7f5f27350
commit
7a9c500e85
1 changed files with 18 additions and 2 deletions
|
|
@ -40,8 +40,24 @@ const getApiBase = (): string => {
|
||||||
return `${backendUrl}/api/v1/migration`;
|
return `${backendUrl}/api/v1/migration`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getAuthToken = (): string | null => {
|
||||||
|
let token = getAccessToken();
|
||||||
|
if (!token && typeof window !== 'undefined') {
|
||||||
|
token =
|
||||||
|
localStorage.getItem('access_token') ||
|
||||||
|
localStorage.getItem('token') ||
|
||||||
|
sessionStorage.getItem('access_token') ||
|
||||||
|
sessionStorage.getItem('token');
|
||||||
|
if (!token && document.cookie) {
|
||||||
|
const match = document.cookie.match(/(?:^|;\s*)(?:access_token|token|jwt)=([^;]+)/);
|
||||||
|
if (match) token = decodeURIComponent(match[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return token;
|
||||||
|
};
|
||||||
|
|
||||||
const authFetch = async (url: string, init?: RequestInit): Promise<Response> => {
|
const authFetch = async (url: string, init?: RequestInit): Promise<Response> => {
|
||||||
const token = getAccessToken();
|
const token = getAuthToken();
|
||||||
const headers = new Headers(init?.headers || {});
|
const headers = new Headers(init?.headers || {});
|
||||||
if (token) {
|
if (token) {
|
||||||
headers.set('Authorization', `Bearer ${token}`);
|
headers.set('Authorization', `Bearer ${token}`);
|
||||||
|
|
@ -288,7 +304,7 @@ export default function MigrationPage() {
|
||||||
|
|
||||||
// Connect SSE Live Stream
|
// Connect SSE Live Stream
|
||||||
if (activeId) {
|
if (activeId) {
|
||||||
const token = getAccessToken();
|
const token = getAuthToken();
|
||||||
const sseUrl = `${apiBase}/jobs/${activeId}/stream${token ? `?token=${encodeURIComponent(token)}` : ''}`;
|
const sseUrl = `${apiBase}/jobs/${activeId}/stream${token ? `?token=${encodeURIComponent(token)}` : ''}`;
|
||||||
const eventSource = new EventSource(sseUrl);
|
const eventSource = new EventSource(sseUrl);
|
||||||
eventSource.onmessage = (event) => {
|
eventSource.onmessage = (event) => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue