38 lines
1.0 KiB
Vue
38 lines
1.0 KiB
Vue
<script setup>
|
|
import axios from 'axios';
|
|
import Cookies from 'js-cookie';
|
|
|
|
axios.defaults.baseURL = import.meta.env.VITE_API_URL;
|
|
axios.defaults.withCredentials = true;
|
|
|
|
// Global Axios Request Interceptor for Authentication
|
|
axios.interceptors.request.use((config) => {
|
|
config.withCredentials = true;
|
|
|
|
// Resolve token from Cookies, localStorage, or document.cookie
|
|
let token = Cookies.get('bm-token') || (typeof localStorage !== 'undefined' ? localStorage.getItem('bm-token') : null);
|
|
|
|
if (!token && typeof document !== 'undefined') {
|
|
token = document.cookie
|
|
.split('; ')
|
|
.find((row) => row.startsWith('bm-token='))
|
|
?.split('=')[1];
|
|
}
|
|
|
|
if (token && token !== 'undefined' && token !== 'null' && token.trim() !== '') {
|
|
config.headers = config.headers || {};
|
|
config.headers.Authorization = `Bearer ${token.trim()}`;
|
|
}
|
|
|
|
return config;
|
|
}, (error) => {
|
|
return Promise.reject(error);
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<router-view />
|
|
</template>
|
|
|
|
<style scoped></style>
|