fix(migration): attach auth token to all fetch and SSE requests
This commit is contained in:
parent
2309e40871
commit
049f68e988
3 changed files with 5286 additions and 5185 deletions
|
|
@ -24,7 +24,6 @@ import {
|
|||
} from 'lucide-react';
|
||||
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';
|
||||
|
||||
|
|
@ -40,8 +39,24 @@ const getApiBase = (): string => {
|
|||
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 token = getAccessToken();
|
||||
const token = getAuthToken();
|
||||
const headers = new Headers(init?.headers || {});
|
||||
if (token) {
|
||||
headers.set('Authorization', `Bearer ${token}`);
|
||||
|
|
@ -288,7 +303,7 @@ export default function MigrationPage() {
|
|||
|
||||
// Connect SSE Live Stream
|
||||
if (activeId) {
|
||||
const token = getAccessToken();
|
||||
const token = getAuthToken();
|
||||
const sseUrl = `${apiBase}/jobs/${activeId}/stream${token ? `?token=${encodeURIComponent(token)}` : ''}`;
|
||||
const eventSource = new EventSource(sseUrl);
|
||||
eventSource.onmessage = (event) => {
|
||||
|
|
@ -328,13 +343,15 @@ export default function MigrationPage() {
|
|||
|
||||
const handleConfirmDeleteMediaGroup = async () => {
|
||||
if (!deleteTarget) return;
|
||||
setMediaDeleting(deleteTarget.id);
|
||||
const target = deleteTarget;
|
||||
setMediaDeleting(target.id);
|
||||
try {
|
||||
const res = await authFetch(`${getApiBase()}/media-groups/${deleteTarget.id}`, { method: 'DELETE' });
|
||||
const res = await authFetch(`${getApiBase()}/media-groups/${target.id}`, { method: 'DELETE' });
|
||||
const data = await res.json();
|
||||
toast.success(data.message || 'Media key deleted');
|
||||
setSelectedMediaGroupIds((ids) => ids.filter((id) => id !== deleteTarget.id));
|
||||
setSelectedMediaGroupIds((ids) => ids.filter((id) => id !== target.id));
|
||||
setDeleteTarget(null);
|
||||
setMediaPreview((prev) => prev.filter((item) => item.id !== target.id && item.media_key !== target.id && item.media_key !== target.name));
|
||||
fetchMediaPreview();
|
||||
} catch {
|
||||
toast.error('Failed to delete media key');
|
||||
|
|
@ -346,16 +363,18 @@ export default function MigrationPage() {
|
|||
const handleConfirmBulkDelete = async () => {
|
||||
if (selectedMediaGroupIds.length === 0) return;
|
||||
setBulkMediaDeleting(true);
|
||||
const toDeleteIds = [...selectedMediaGroupIds];
|
||||
try {
|
||||
const res = await authFetch(`${getApiBase()}/media-groups/bulk-delete`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ group_ids: selectedMediaGroupIds }),
|
||||
body: JSON.stringify({ group_ids: toDeleteIds, ids: toDeleteIds }),
|
||||
});
|
||||
const data = await res.json();
|
||||
toast.success(data.message || `Deleted ${data.deleted_groups_count || selectedMediaGroupIds.length} media key(s)`);
|
||||
toast.success(data.message || `Deleted ${data.deleted_groups_count || toDeleteIds.length} media key(s)`);
|
||||
setSelectedMediaGroupIds([]);
|
||||
setShowBulkDelete(false);
|
||||
setMediaPreview((prev) => prev.filter((item) => !toDeleteIds.includes(item.id) && !toDeleteIds.includes(item.media_key)));
|
||||
fetchMediaPreview();
|
||||
} catch {
|
||||
toast.error('Bulk delete failed');
|
||||
|
|
@ -561,6 +580,9 @@ export default function MigrationPage() {
|
|||
<form onSubmit={handleStartImport} className="p-5 space-y-5">
|
||||
<div>
|
||||
<h2 className="text-[15px] font-semibold text-foreground">Upload Catalog Spreadsheet & Optional Media ZIP</h2>
|
||||
<p className="text-[13px] text-muted-foreground mt-1">
|
||||
Spreadsheet is required for product import. The system will automatically parse and link catalog items.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
|
|
@ -613,16 +635,14 @@ export default function MigrationPage() {
|
|||
<Upload className="w-3.5 h-3.5" />
|
||||
Choose file
|
||||
</button>
|
||||
<CustomSelect
|
||||
size="sm"
|
||||
<select
|
||||
value={mediaStructure}
|
||||
onChange={(value) => setMediaStructure(value as 'AUTO' | 'FLAT')}
|
||||
options={[
|
||||
{ value: 'AUTO', label: 'Subfolder Mode (Default)' },
|
||||
{ value: 'FLAT', label: 'Flat File Mode (key__01.png)' },
|
||||
]}
|
||||
className="w-[240px]"
|
||||
/>
|
||||
onChange={(e) => setMediaStructure(e.target.value as 'AUTO' | 'FLAT')}
|
||||
className="h-8 px-2.5 bg-card border border-border crm-radius-control text-[12px] font-medium text-foreground focus:outline-none focus:border-primary cursor-pointer"
|
||||
>
|
||||
<option value="AUTO">Subfolder Mode (Default)</option>
|
||||
<option value="FLAT">Flat File Mode (key__01.png)</option>
|
||||
</select>
|
||||
</div>
|
||||
{selectedMediaFile && (
|
||||
<p className="text-[12px] text-foreground font-medium truncate">
|
||||
|
|
@ -835,6 +855,7 @@ export default function MigrationPage() {
|
|||
<div className="px-5 py-4 border-b border-border flex flex-wrap items-center justify-between gap-3">
|
||||
<div>
|
||||
<h2 className="text-[15px] font-semibold text-foreground">Media Library</h2>
|
||||
<p className="text-[13px] text-muted-foreground mt-0.5">Media keys indexed from uploaded ZIP archives.</p>
|
||||
</div>
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
{selectedMediaGroupIds.length > 0 && (
|
||||
|
|
@ -954,6 +975,9 @@ export default function MigrationPage() {
|
|||
<div className="px-5 py-4 border-b border-border flex flex-wrap items-center justify-between gap-3">
|
||||
<div>
|
||||
<h2 className="text-[15px] font-semibold text-foreground">Import History & Audit Logs</h2>
|
||||
<p className="text-[13px] text-muted-foreground mt-0.5">
|
||||
Full historical audit trail of created SKUs, updated records, execution time, and rollback actions.
|
||||
</p>
|
||||
</div>
|
||||
<button type="button" onClick={() => setShowPurgeConfirm(true)} disabled={purgeLoading} className={dangerButton}>
|
||||
<Trash2 className="w-3.5 h-3.5" />
|
||||
|
|
|
|||
1
next-env.d.ts
vendored
1
next-env.d.ts
vendored
|
|
@ -1,5 +1,6 @@
|
|||
/// <reference types="next" />
|
||||
/// <reference types="next/image-types/global" />
|
||||
/// <reference path="./.next/types/routes.d.ts" />
|
||||
|
||||
// NOTE: This file should not be edited
|
||||
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
|
||||
|
|
|
|||
10412
pnpm-lock.yaml
10412
pnpm-lock.yaml
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue