From 30c42324068a7d586c35f8920abe6e4ce34797db Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 29 Oct 2025 20:58:02 +0300 Subject: [PATCH] post requests fixed --- src/services/apiClient.js | 52 +++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/src/services/apiClient.js b/src/services/apiClient.js index 60d5d46..7bc1f10 100644 --- a/src/services/apiClient.js +++ b/src/services/apiClient.js @@ -6,23 +6,23 @@ let isRefreshing = false let refreshPromise = null function createConfig(options = {}) { - const authStore = useAuthStore() + const token = localStorage.getItem('accessToken'); const headers = { 'Content-Type': 'application/json', ...options.headers, - } + }; - if (authStore.accessToken) { - headers['Authorization'] = `Bearer ${authStore.accessToken}` + if (token) { + headers['Authorization'] = `Bearer ${token}`; } return { headers, - credentials: 'include', ...options, - } + }; } + async function refreshAccessToken() { const authStore = useAuthStore() @@ -53,7 +53,8 @@ async function refreshAccessToken() { }) .catch((error) => { - throw error + console.error('Refresh error:', error) + throw new Error('REFRESH_FAILED') }) .finally(() => { @@ -66,29 +67,32 @@ async function refreshAccessToken() { async function request(url, options = {}, isRetry = false) { const config = createConfig(options) - const response = await fetch(`${BASE_URL}${url}`, config) if (response.status === 401 && !isRetry) { try { const data = await refreshAccessToken() - const token = data.access_token - if (!token) { - throw new Error('Refresh response did not contain access_token') - } + if (!token) throw new Error('Refresh response did not contain access_token') + const newOptions = { ...options, headers: { ...options.headers, - 'Authorization': `Bearer ${token}`, + Authorization: `Bearer ${token}`, }, } + return await request(url, newOptions, true) } catch (e) { - const authStore = useAuthStore() - authStore.forceLogout() + if (e.message === 'REFRESH_FAILED') { + const authStore = useAuthStore() + authStore.forceLogout() + console.warn('Force logout (refresh failed)', url) + } else { + console.error('Unexpected error during refresh', e) + } throw e } } @@ -100,9 +104,15 @@ async function request(url, options = {}, isRetry = false) { } catch { errorData = {} } - throw new Error(errorData.message || `HTTP Error: ${response.status}`) + + return { + status: response.status, + ok: false, + error: errorData.message || `HTTP Error: ${response.status}`, + } } + let data = null const contentType = response.headers.get('content-type') if (contentType?.includes('application/json')) { @@ -113,11 +123,7 @@ async function request(url, options = {}, isRetry = false) { } } - return { - status: response.status, - ok: response.ok, - data, - } + return { status: response.status, ok: response.ok, data } } export const apiClient = { @@ -132,9 +138,7 @@ export const apiClient = { return request(url, { method: 'POST', body: isFormData ? data : JSON.stringify(data), - headers: isFormData - ? {} - : { 'Content-Type': 'application/json' } + // headers: isFormData ? {} : { 'Content-Type': 'application/json' } }) }, put: (url, data) => request(url, {