post requests fixed
This commit is contained in:
parent
13328aeec3
commit
30c4232406
1 changed files with 28 additions and 24 deletions
|
|
@ -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, {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue