Files
myocardial/resources/js/plugins/axios.js
T
2026-06-23 13:28:38 +07:00

53 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// eslint-disable-next-line regex/invalid
import axios from 'axios'
const axiosIns = axios.create({
// You can add your headers here
// ================================
// baseURL: 'https://some-domain.com/api/',
// timeout: 1000,
// headers: {'X-Custom-Header': 'foobar'}
})
// ️ Add request interceptor to send the authorization header on each subsequent request after login
axiosIns.interceptors.request.use(config => {
// Retrieve token from localStorage
const token = localStorage.getItem('access_token')
// If token is found
if (token) {
// Get request headers and if headers is undefined assign blank object
config.headers = config.headers || {}
// Set authorization header
config.headers.Authorization = token ? `Bearer ${token}` : ''
}
// Return modified config
return config
})
// ️ Add response interceptor to handle 401 response
// axiosIns.interceptors.response.use(response => {
// return response
// }, error => {
// // Handle error
// if (error.response.status === 401) {
// // ️ Logout user and redirect to login page
// // Remove "userData" from localStorage
// // localStorage.removeItem('userData')
// // Remove "accessToken" from localStorage
// localStorage.removeItem('access_token')
// // If 401 response returned from api
// router.push('/login')
// }
// else {
// return Promise.reject(error)
// }
// })
export default axiosIns