From 0d7ef72e9509650e0bbd8981e7bd4576d4213b3b Mon Sep 17 00:00:00 2001 From: rcourtman Date: Tue, 23 Dec 2025 12:24:43 +0000 Subject: [PATCH] Polish: Standardize API calls in Settings (Import/Export) --- .../src/components/SecurityWarning.tsx | 1 - .../src/components/Settings/Settings.tsx | 60 +------------------ 2 files changed, 2 insertions(+), 59 deletions(-) diff --git a/frontend-modern/src/components/SecurityWarning.tsx b/frontend-modern/src/components/SecurityWarning.tsx index 311d5c47f..606ad35a7 100644 --- a/frontend-modern/src/components/SecurityWarning.tsx +++ b/frontend-modern/src/components/SecurityWarning.tsx @@ -2,7 +2,6 @@ import { Component, createSignal, Show, onMount } from 'solid-js'; import { Portal } from 'solid-js/web'; import { SectionHeader } from '@/components/shared/SectionHeader'; import { isPulseHttps } from '@/utils/url'; -import { isPulseHttps } from '@/utils/url'; import { logger } from '@/utils/logger'; import { apiFetchJSON } from '@/utils/apiClient'; diff --git a/frontend-modern/src/components/Settings/Settings.tsx b/frontend-modern/src/components/Settings/Settings.tsx index e04bf393f..bcd544b88 100644 --- a/frontend-modern/src/components/Settings/Settings.tsx +++ b/frontend-modern/src/components/Settings/Settings.tsx @@ -2129,42 +2129,11 @@ const Settings: Component = (props) => { headers['X-API-Token'] = apiToken; } - const response = await fetch('/api/config/export', { + const data = await apiFetchJSON('/api/config/export', { method: 'POST', - headers, - credentials: 'include', // Include cookies for session auth body: JSON.stringify({ passphrase: exportPassphrase() }), }); - if (!response.ok) { - const errorText = await response.text(); - // Handle authentication errors - if (response.status === 401 || response.status === 403) { - // Check if we're using API token auth (not password auth) - const hasPasswordAuth = securityStatus()?.hasAuthentication; - if (!hasPasswordAuth) { - // Clear invalid token if we had one - const hadToken = getApiClientToken(); - if (hadToken) { - clearApiClientToken(); - notificationStore.error('Invalid or expired API token. Please re-enter.'); - setApiTokenModalSource('export'); - setShowApiTokenModal(true); - return; - } - if (errorText.includes('API_TOKEN') || errorText.includes('API_TOKENS')) { - setApiTokenModalSource('export'); - setShowApiTokenModal(true); - return; - } - } - throw new Error('Export requires authentication'); - } - throw new Error(errorText || 'Export failed'); - } - - const data = await response.json(); - // Create and download file const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }); const url = URL.createObjectURL(blob); @@ -2235,33 +2204,8 @@ const Settings: Component = (props) => { encryptedData = fileContent.trim(); } - // Get CSRF token from cookie - const csrfCookie = document.cookie - .split('; ') - .find((row) => row.startsWith('pulse_csrf=')); - const csrfToken = csrfCookie - ? decodeURIComponent(csrfCookie.split('=').slice(1).join('=')) - : undefined; - - const headers: HeadersInit = { - 'Content-Type': 'application/json', - }; - - // Add CSRF token if available - if (csrfToken) { - headers['X-CSRF-Token'] = csrfToken; - } - - // Add API token if configured - const apiToken = getApiClientToken(); - if (apiToken) { - headers['X-API-Token'] = apiToken; - } - - const response = await fetch('/api/config/import', { + await apiFetchJSON('/api/config/import', { method: 'POST', - headers, - credentials: 'include', // Include cookies for session auth body: JSON.stringify({ passphrase: importPassphrase(), data: encryptedData,