post requests fixed

This commit is contained in:
nquidox 2025-10-29 20:58:02 +03:00
parent 13328aeec3
commit 30c4232406

View file

@ -6,23 +6,23 @@ let isRefreshing = false
let refreshPromise = null let refreshPromise = null
function createConfig(options = {}) { function createConfig(options = {}) {
const authStore = useAuthStore() const token = localStorage.getItem('accessToken');
const headers = { const headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
...options.headers, ...options.headers,
} };
if (authStore.accessToken) { if (token) {
headers['Authorization'] = `Bearer ${authStore.accessToken}` headers['Authorization'] = `Bearer ${token}`;
} }
return { return {
headers, headers,
credentials: 'include',
...options, ...options,
} };
} }
async function refreshAccessToken() { async function refreshAccessToken() {
const authStore = useAuthStore() const authStore = useAuthStore()
@ -53,7 +53,8 @@ async function refreshAccessToken() {
}) })
.catch((error) => { .catch((error) => {
throw error console.error('Refresh error:', error)
throw new Error('REFRESH_FAILED')
}) })
.finally(() => { .finally(() => {
@ -66,29 +67,32 @@ async function refreshAccessToken() {
async function request(url, options = {}, isRetry = false) { async function request(url, options = {}, isRetry = false) {
const config = createConfig(options) const config = createConfig(options)
const response = await fetch(`${BASE_URL}${url}`, config) const response = await fetch(`${BASE_URL}${url}`, config)
if (response.status === 401 && !isRetry) { if (response.status === 401 && !isRetry) {
try { try {
const data = await refreshAccessToken() const data = await refreshAccessToken()
const token = data.access_token const token = data.access_token
if (!token) { if (!token) throw new Error('Refresh response did not contain access_token')
throw new Error('Refresh response did not contain access_token')
}
const newOptions = { const newOptions = {
...options, ...options,
headers: { headers: {
...options.headers, ...options.headers,
'Authorization': `Bearer ${token}`, Authorization: `Bearer ${token}`,
}, },
} }
return await request(url, newOptions, true) return await request(url, newOptions, true)
} catch (e) { } catch (e) {
const authStore = useAuthStore() if (e.message === 'REFRESH_FAILED') {
authStore.forceLogout() const authStore = useAuthStore()
authStore.forceLogout()
console.warn('Force logout (refresh failed)', url)
} else {
console.error('Unexpected error during refresh', e)
}
throw e throw e
} }
} }
@ -100,9 +104,15 @@ async function request(url, options = {}, isRetry = false) {
} catch { } catch {
errorData = {} 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 let data = null
const contentType = response.headers.get('content-type') const contentType = response.headers.get('content-type')
if (contentType?.includes('application/json')) { if (contentType?.includes('application/json')) {
@ -113,11 +123,7 @@ async function request(url, options = {}, isRetry = false) {
} }
} }
return { return { status: response.status, ok: response.ok, data }
status: response.status,
ok: response.ok,
data,
}
} }
export const apiClient = { export const apiClient = {
@ -132,9 +138,7 @@ export const apiClient = {
return request(url, { return request(url, {
method: 'POST', method: 'POST',
body: isFormData ? data : JSON.stringify(data), body: isFormData ? data : JSON.stringify(data),
headers: isFormData // headers: isFormData ? {} : { 'Content-Type': 'application/json' }
? {}
: { 'Content-Type': 'application/json' }
}) })
}, },
put: (url, data) => request(url, { put: (url, data) => request(url, {