git-subtree-dir: frontend git-subtree-mainline:205a3bd14bgit-subtree-split:c070c0315d
25 lines
623 B
JavaScript
25 lines
623 B
JavaScript
import axios from "axios"
|
|
import {useAuthStore} from "@/stores/authStore.js"
|
|
|
|
export function useClient() {
|
|
if (!import.meta.env.VITE_API_URL) throw new Error("VITE_API_URL is not provided")
|
|
|
|
const api = axios.create({
|
|
baseURL: import.meta.env.VITE_API_URL,
|
|
timeout: 10000,
|
|
});
|
|
|
|
const authStore = useAuthStore()
|
|
const requestInterceptor = (config) => {
|
|
if (authStore.isAuthenticated) {
|
|
config.headers["Authorization"] = `Bearer ${authStore.accessToken}`
|
|
}
|
|
return config
|
|
}
|
|
|
|
api.interceptors.request.use(requestInterceptor);
|
|
|
|
return api;
|
|
}
|
|
|