Files
fiberops-web/lib/portal-api.ts

34 lines
995 B
TypeScript

import axios from 'axios';
const portalApi = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL || 'http://192.168.1.167:3001',
});
portalApi.interceptors.request.use((config) => {
if (typeof window !== 'undefined') {
const token = localStorage.getItem('portal_token');
const authRaw = localStorage.getItem('portal_auth');
const tenantSlug = authRaw ? JSON.parse(authRaw)?.state?.tenantSlug : null;
if (token) config.headers.Authorization = `Bearer ${token}`;
if (tenantSlug) {
config.headers['x-tenant-slug'] = tenantSlug;
config.headers['X-Tenant-Slug'] = tenantSlug;
}
}
return config;
});
portalApi.interceptors.response.use(
(res) => res,
(err) => {
if (err.response?.status === 401 && typeof window !== 'undefined') {
localStorage.removeItem('portal_token');
localStorage.removeItem('portal_auth');
window.location.href = '/portal/login';
}
return Promise.reject(err);
}
);
export default portalApi;