-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.ts
More file actions
33 lines (29 loc) · 759 Bytes
/
api.ts
File metadata and controls
33 lines (29 loc) · 759 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import axios from 'axios'
import { useAuthStore } from '@/modules/auth/stores/useAuthStore'
import router from '@/router'
export const api = axios.create({
baseURL: import.meta.env.VITE_API_URL || '/api/v1',
headers: {
'Content-Type': 'application/json',
},
})
api.interceptors.request.use(config => {
const auth = useAuthStore()
if (auth.token) {
config.headers.Authorization = `Bearer ${auth.token}`
}
return config
})
api.interceptors.response.use(
response => response,
error => {
if (error.response?.status === 401) {
const auth = useAuthStore()
auth.logout()
if (router.currentRoute.value.name !== 'Login') {
router.push({ name: 'Login' })
}
}
return Promise.reject(error)
}
)