Files
social-media/src/plugins/api.js
2024-08-03 04:15:55 -04:00

27 lines
748 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")
// You create a .env.development file and a .env file
// depending on the environment, the correct file will be used
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;
}