25 lines
619 B
TypeScript
25 lines
619 B
TypeScript
import axios from 'axios'
|
|
|
|
export const apiClient = axios.create({
|
|
baseURL: '/api/v1',
|
|
withCredentials: true,
|
|
withXSRFToken: true,
|
|
headers: { Accept: 'application/json' },
|
|
})
|
|
|
|
export async function ensureCsrfCookie() {
|
|
await axios.get('/sanctum/csrf-cookie', { baseURL: '/', withCredentials: true, withXSRFToken: true })
|
|
}
|
|
|
|
export const UNAUTHORIZED_EVENT = 'auth:unauthorized'
|
|
|
|
apiClient.interceptors.response.use(
|
|
(response) => response,
|
|
(error) => {
|
|
if (error.response?.status === 401) {
|
|
window.dispatchEvent(new CustomEvent(UNAUTHORIZED_EVENT))
|
|
}
|
|
return Promise.reject(error)
|
|
},
|
|
)
|