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 { TablePagination, useClientPagination } from '@/components/ui/TablePagination';
|
||||||
import { CustomSelect } from '@/components/ui/CustomSelect';
|
import { CustomSelect } from '@/components/ui/CustomSelect';
|
||||||
import { getMediaUrl } from '@/services/api/config';
|
import { getMediaUrl } from '@/services/api/config';
|
||||||
|
import { getAccessToken } from '@/services/api/client';
|
||||||
|
|
||||||
const getApiBase = (): string => {
|
const getApiBase = (): string => {
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
|
|
@ -39,6 +40,18 @@ const getApiBase = (): string => {
|
||||||
return `${backendUrl}/api/v1/migration`;
|
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 TabId = 'wizard' | 'media' | 'history';
|
||||||
type ImportMode = 'UPSERT' | 'CREATE_ONLY' | 'UPDATE_EXISTING' | 'SKIP_EXISTING';
|
type ImportMode = 'UPSERT' | 'CREATE_ONLY' | 'UPDATE_EXISTING' | 'SKIP_EXISTING';
|
||||||
|
|
||||||
|
|
@ -153,7 +166,7 @@ export default function MigrationPage() {
|
||||||
const pollStatus = async () => {
|
const pollStatus = async () => {
|
||||||
try {
|
try {
|
||||||
const apiBase = getApiBase();
|
const apiBase = getApiBase();
|
||||||
const res = await fetch(`${apiBase}/jobs/${activeJobId}/status`);
|
const res = await authFetch(`${apiBase}/jobs/${activeJobId}/status`);
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
if (data && (data.status === 'success' || data.job_status)) {
|
if (data && (data.status === 'success' || data.job_status)) {
|
||||||
setSseTelemetry(data);
|
setSseTelemetry(data);
|
||||||
|
|
@ -174,7 +187,7 @@ export default function MigrationPage() {
|
||||||
const fetchBatches = async () => {
|
const fetchBatches = async () => {
|
||||||
setHistoryLoading(true);
|
setHistoryLoading(true);
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${getApiBase()}/batches`);
|
const res = await authFetch(`${getApiBase()}/batches`);
|
||||||
if (!res.ok) throw new Error(`HTTP ${res.status}: ${res.statusText}`);
|
if (!res.ok) throw new Error(`HTTP ${res.status}: ${res.statusText}`);
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
if (data.status === 'success') {
|
if (data.status === 'success') {
|
||||||
|
|
@ -191,7 +204,7 @@ export default function MigrationPage() {
|
||||||
// Fetch Media Groups Preview
|
// Fetch Media Groups Preview
|
||||||
const fetchMediaPreview = async () => {
|
const fetchMediaPreview = async () => {
|
||||||
try {
|
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}`);
|
if (!res.ok) throw new Error(`HTTP ${res.status}: ${res.statusText}`);
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
if (data.status === 'success') {
|
if (data.status === 'success') {
|
||||||
|
|
@ -226,7 +239,7 @@ export default function MigrationPage() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const apiBase = getApiBase();
|
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 = {};
|
let data: any = {};
|
||||||
try {
|
try {
|
||||||
|
|
@ -256,7 +269,7 @@ export default function MigrationPage() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute migration immediately for spreadsheet file
|
// 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 = {};
|
let execData: any = {};
|
||||||
try {
|
try {
|
||||||
execData = await execRes.json();
|
execData = await execRes.json();
|
||||||
|
|
@ -275,7 +288,9 @@ export default function MigrationPage() {
|
||||||
|
|
||||||
// Connect SSE Live Stream
|
// Connect SSE Live Stream
|
||||||
if (activeId) {
|
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) => {
|
eventSource.onmessage = (event) => {
|
||||||
try {
|
try {
|
||||||
const streamData = JSON.parse(event.data);
|
const streamData = JSON.parse(event.data);
|
||||||
|
|
@ -315,7 +330,7 @@ export default function MigrationPage() {
|
||||||
if (!deleteTarget) return;
|
if (!deleteTarget) return;
|
||||||
setMediaDeleting(deleteTarget.id);
|
setMediaDeleting(deleteTarget.id);
|
||||||
try {
|
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();
|
const data = await res.json();
|
||||||
toast.success(data.message || 'Media key deleted');
|
toast.success(data.message || 'Media key deleted');
|
||||||
setSelectedMediaGroupIds((ids) => ids.filter((id) => id !== deleteTarget.id));
|
setSelectedMediaGroupIds((ids) => ids.filter((id) => id !== deleteTarget.id));
|
||||||
|
|
@ -332,7 +347,7 @@ export default function MigrationPage() {
|
||||||
if (selectedMediaGroupIds.length === 0) return;
|
if (selectedMediaGroupIds.length === 0) return;
|
||||||
setBulkMediaDeleting(true);
|
setBulkMediaDeleting(true);
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${getApiBase()}/media-groups/bulk-delete`, {
|
const res = await authFetch(`${getApiBase()}/media-groups/bulk-delete`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ group_ids: selectedMediaGroupIds }),
|
body: JSON.stringify({ group_ids: selectedMediaGroupIds }),
|
||||||
|
|
@ -353,7 +368,7 @@ export default function MigrationPage() {
|
||||||
if (!rollbackTarget) return;
|
if (!rollbackTarget) return;
|
||||||
setRollbackLoading(rollbackTarget.id);
|
setRollbackLoading(rollbackTarget.id);
|
||||||
try {
|
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();
|
const data = await res.json();
|
||||||
toast.success(data.message || 'Rollback completed');
|
toast.success(data.message || 'Rollback completed');
|
||||||
setRollbackTarget(null);
|
setRollbackTarget(null);
|
||||||
|
|
@ -368,7 +383,7 @@ export default function MigrationPage() {
|
||||||
const handleConfirmPurge = async () => {
|
const handleConfirmPurge = async () => {
|
||||||
setPurgeLoading(true);
|
setPurgeLoading(true);
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${getApiBase()}/purge-all`, { method: 'POST' });
|
const res = await authFetch(`${getApiBase()}/purge-all`, { method: 'POST' });
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
toast.success(data.message || 'Catalog and media files purged');
|
toast.success(data.message || 'Catalog and media files purged');
|
||||||
setMediaPreview([]);
|
setMediaPreview([]);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue