2025-08-05 12:35:40 +07:00
|
|
|
import axios from 'axios'
|
2025-08-14 03:09:00 +07:00
|
|
|
import { toast } from 'react-toastify'
|
2025-08-05 12:35:40 +07:00
|
|
|
|
|
|
|
|
const getToken = () => {
|
|
|
|
|
return localStorage.getItem('authToken')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const api = axios.create({
|
2025-09-25 20:37:47 +07:00
|
|
|
baseURL: 'https://api-pos.apskel.id/api/v1',
|
|
|
|
|
// baseURL: 'http://127.0.0.1:4000/api/v1',
|
2025-08-05 12:35:40 +07:00
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
timeout: 10000
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
api.interceptors.request.use(
|
|
|
|
|
config => {
|
|
|
|
|
const token = getToken()
|
|
|
|
|
if (token) {
|
|
|
|
|
config.headers.Authorization = `Bearer ${token}`
|
|
|
|
|
}
|
|
|
|
|
return config
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
api.interceptors.response.use(
|
|
|
|
|
response => response,
|
|
|
|
|
error => {
|
|
|
|
|
const status = error.response?.status
|
|
|
|
|
|
2025-09-18 11:00:00 +07:00
|
|
|
if (status === 401) {
|
2025-09-10 14:24:18 +07:00
|
|
|
localStorage.removeItem('user')
|
|
|
|
|
localStorage.removeItem('authToken')
|
2025-08-05 12:35:40 +07:00
|
|
|
window.location.href = '/login'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status === 403) {
|
|
|
|
|
console.error('Forbidden: Kamu tidak punya akses.')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status >= 500) {
|
2025-08-14 03:09:00 +07:00
|
|
|
toast.error(error.response?.data?.errors[0].cause || 'Terjadi kesalahan server.')
|
2025-08-05 12:35:40 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
}
|
|
|
|
|
)
|