Files
social-media/src/plugins/clientPlugin.js
2024-03-11 22:10:36 -04:00

34 lines
908 B
JavaScript

import axios from "axios";
import { inject } from 'vue';
const clientKey = Symbol('client');
export default {
//todo: Need to have the baseURL in the config for later ( dev and prod env. )
install: (app) => {
const axiosInstance = axios.create({
baseURL: 'https://localhost:5001/',
timeout: 2000,
});
axiosInstance.interceptors.request.use((config) => {
const token = localStorage.getItem('jwt');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
}, (error) => {
return Promise.reject(error);
});
app.provide(clientKey, axiosInstance);
}
};
export function useClient() {
const client = inject(clientKey);
if (!client) {
throw new Error('Axios instance is not provided');
}
return client;
}