import axios from "axios"

const API = axios.create({
    baseURL: "http://localhost:1234/api"
})

API.interceptors.request.use((config) => {
    const token = localStorage.getItem("token")

    if (token) {
        config.headers.Authorization = `Bearer ${token}`
    }

    return config
}, (error) => {
    return Promise.reject(error)
})

API.interceptors.request.use((response) => response,
    (error) => {
        if (error.response?.status == 401) {
            console.log("Unauthorized - login again");
        }
        return Promise.reject(error)
    })

export default API