fix(migration): attach Authorization Bearer token to all migration API requests and SSE stream
This commit is contained in:
parent
1d0e9a2e07
commit
2309e40871
1 changed files with 25 additions and 10 deletions
|
|
@ -26,6 +26,7 @@ import { toast } from 'sonner';
|
|||
import { TablePagination, useClientPagination } from '@/components/ui/TablePagination';
|
||||
import { CustomSelect } from '@/components/ui/CustomSelect';
|
||||
import { getMediaUrl } from '@/services/api/config';
|
||||
import { getAccessToken } from '@/services/api/client';
|
||||
|
||||
const getApiBase = (): string => {
|
||||
if (typeof window !== 'undefined') {
|
||||
|
|
@ -39,6 +40,18 @@ const getApiBase = (): string => {
|
|||
return `${backendUrl}/api/v1/migration`;
|
||||
};
|
||||
|
||||
const authFetch = async (url: string, init?: RequestInit): Promise<Response> => {
|
||||
const token = getAccessToken();
|
||||
const headers = new Headers(init?.headers || {});
|
||||
if (token) {
|
||||
headers.set('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
return fetch(url, {
|
||||
...init,
|
||||
headers,
|
||||
});
|
||||
};
|
||||
|
||||
type TabId = 'wizard' | 'media' | 'history';
|
||||
type ImportMode = 'UPSERT' | 'CREATE_ONLY' | 'UPDATE_EXISTING' | 'SKIP_EXISTING';
|
||||
|
||||
|
|
@ -153,7 +166,7 @@ export default function MigrationPage() {
|
|||
const pollStatus = async () => {
|
||||
try {
|
||||
const apiBase = getApiBase();
|
||||
const res = await fetch(`${apiBase}/jobs/${activeJobId}/status`);
|
||||
const res = await authFetch(`${apiBase}/jobs/${activeJobId}/status`);
|
||||
const data = await res.json();
|
||||
if (data && (data.status === 'success' || data.job_status)) {
|
||||
setSseTelemetry(data);
|
||||
|
|
@ -174,7 +187,7 @@ export default function MigrationPage() {
|
|||
const fetchBatches = async () => {
|
||||
setHistoryLoading(true);
|
||||
try {
|
||||
const res = await fetch(`${getApiBase()}/batches`);
|
||||
const res = await authFetch(`${getApiBase()}/batches`);
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}: ${res.statusText}`);
|
||||
const data = await res.json();
|
||||
if (data.status === 'success') {
|
||||
|
|
@ -191,7 +204,7 @@ export default function MigrationPage() {
|
|||
// Fetch Media Groups Preview
|
||||
const fetchMediaPreview = async () => {
|
||||
try {
|
||||
const res = await fetch(`${getApiBase()}/media-groups/preview`);
|
||||
const res = await authFetch(`${getApiBase()}/media-groups/preview`);
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}: ${res.statusText}`);
|
||||
const data = await res.json();
|
||||
if (data.status === 'success') {
|
||||
|
|
@ -226,7 +239,7 @@ export default function MigrationPage() {
|
|||
|
||||
try {
|
||||
const apiBase = getApiBase();
|
||||
const res = await fetch(`${apiBase}/upload`, { method: 'POST', body: formData });
|
||||
const res = await authFetch(`${apiBase}/upload`, { method: 'POST', body: formData });
|
||||
|
||||
let data: any = {};
|
||||
try {
|
||||
|
|
@ -256,7 +269,7 @@ export default function MigrationPage() {
|
|||
}
|
||||
|
||||
// Execute migration immediately for spreadsheet file
|
||||
const execRes = await fetch(`${apiBase}/execute?job_id=${jobId}`, { method: 'POST' });
|
||||
const execRes = await authFetch(`${apiBase}/execute?job_id=${jobId}`, { method: 'POST' });
|
||||
let execData: any = {};
|
||||
try {
|
||||
execData = await execRes.json();
|
||||
|
|
@ -275,7 +288,9 @@ export default function MigrationPage() {
|
|||
|
||||
// Connect SSE Live Stream
|
||||
if (activeId) {
|
||||
const eventSource = new EventSource(`${apiBase}/jobs/${activeId}/stream`);
|
||||
const token = getAccessToken();
|
||||
const sseUrl = `${apiBase}/jobs/${activeId}/stream${token ? `?token=${encodeURIComponent(token)}` : ''}`;
|
||||
const eventSource = new EventSource(sseUrl);
|
||||
eventSource.onmessage = (event) => {
|
||||
try {
|
||||
const streamData = JSON.parse(event.data);
|
||||
|
|
@ -315,7 +330,7 @@ export default function MigrationPage() {
|
|||
if (!deleteTarget) return;
|
||||
setMediaDeleting(deleteTarget.id);
|
||||
try {
|
||||
const res = await fetch(`${getApiBase()}/media-groups/${deleteTarget.id}`, { method: 'DELETE' });
|
||||
const res = await authFetch(`${getApiBase()}/media-groups/${deleteTarget.id}`, { method: 'DELETE' });
|
||||
const data = await res.json();
|
||||
toast.success(data.message || 'Media key deleted');
|
||||
setSelectedMediaGroupIds((ids) => ids.filter((id) => id !== deleteTarget.id));
|
||||
|
|
@ -332,7 +347,7 @@ export default function MigrationPage() {
|
|||
if (selectedMediaGroupIds.length === 0) return;
|
||||
setBulkMediaDeleting(true);
|
||||
try {
|
||||
const res = await fetch(`${getApiBase()}/media-groups/bulk-delete`, {
|
||||
const res = await authFetch(`${getApiBase()}/media-groups/bulk-delete`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ group_ids: selectedMediaGroupIds }),
|
||||
|
|
@ -353,7 +368,7 @@ export default function MigrationPage() {
|
|||
if (!rollbackTarget) return;
|
||||
setRollbackLoading(rollbackTarget.id);
|
||||
try {
|
||||
const res = await fetch(`${getApiBase()}/rollback/${rollbackTarget.id}`, { method: 'POST' });
|
||||
const res = await authFetch(`${getApiBase()}/rollback/${rollbackTarget.id}`, { method: 'POST' });
|
||||
const data = await res.json();
|
||||
toast.success(data.message || 'Rollback completed');
|
||||
setRollbackTarget(null);
|
||||
|
|
@ -368,7 +383,7 @@ export default function MigrationPage() {
|
|||
const handleConfirmPurge = async () => {
|
||||
setPurgeLoading(true);
|
||||
try {
|
||||
const res = await fetch(`${getApiBase()}/purge-all`, { method: 'POST' });
|
||||
const res = await authFetch(`${getApiBase()}/purge-all`, { method: 'POST' });
|
||||
const data = await res.json();
|
||||
toast.success(data.message || 'Catalog and media files purged');
|
||||
setMediaPreview([]);
|
||||
|
|
|
|||
Loading…
Reference in a new issue