import axios from "axios"; const TOKEN_KEY = "pdfflow_token"; export const api = axios.create({ baseURL: process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000", }); // Attach JWT token to every request api.interceptors.request.use((config) => { if (typeof window !== "undefined") { const token = localStorage.getItem(TOKEN_KEY); if (token) { config.headers.Authorization = `Bearer ${token}`; } } return config; }); // Handle 401 responses — clear stale token only. // No-login model: tools work without auth, so a 401 must NOT force a redirect // to signin. We just drop any stale/expired token and let the caller handle // the error inline (e.g. an account-only page can prompt to sign in). api.interceptors.response.use( (response) => response, (error) => { if ( error.response?.status === 401 && typeof window !== "undefined" ) { localStorage.removeItem(TOKEN_KEY); } return Promise.reject(error); } );