Added client to call backend authentication logic
This commit is contained in:
34
src/plugins/clientPlugin.js
Normal file
34
src/plugins/clientPlugin.js
Normal file
@@ -0,0 +1,34 @@
|
||||
import axios from "axios";
|
||||
import { inject } from 'vue';
|
||||
|
||||
const clientKey = Symbol('client');
|
||||
|
||||
export default {
|
||||
install: (app) => {
|
||||
const axiosInstance = axios.create({
|
||||
baseURL: 'https://localhost:5001/',
|
||||
timeout: 5000,
|
||||
});
|
||||
|
||||
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);
|
||||
console.log("client provided");
|
||||
}
|
||||
};
|
||||
|
||||
export function useClient() {
|
||||
const client = inject(clientKey);
|
||||
if (!client) {
|
||||
throw new Error('Axios instance is not provided');
|
||||
}
|
||||
return client;
|
||||
}
|
||||
Reference in New Issue
Block a user