Initial commit

This commit is contained in:
2026-03-05 15:14:22 +03:00
commit 3aa07d096f
167 changed files with 23898 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
import axios, { AxiosInstance } from 'axios';
import { getSession, signOut } from 'next-auth/react';
// const MESSAGES = {
// tr: {
// title: 'Oturum Süresi Doldu',
// description: 'Güvenliğiniz için çıkış yapıldı. Lütfen tekrar giriş yapınız.',
// },
// en: {
// title: 'Session Expired',
// description: 'You have been logged out for security reasons. Please log in again.',
// },
// };
export function createApiClient(baseURL: string): AxiosInstance {
const client = axios.create({ baseURL });
// Helper to get locale from cookie or URL
const getLocale = (): string => {
if (typeof window !== 'undefined') {
// Try to get from cookie first (NEXT_LOCALE is the default cookie name)
const cookieLocale = document.cookie
.split('; ')
.find((row) => row.startsWith('NEXT_LOCALE='))
?.split('=')[1];
if (cookieLocale) return cookieLocale;
// Fallback: get from URL path (e.g., /tr/generator -> tr)
const pathLocale = window.location.pathname.split('/')[1];
if (['en', 'tr', 'de'].includes(pathLocale)) return pathLocale;
}
return 'tr'; // Default locale
};
client.interceptors.request.use(async (config) => {
const session = await getSession();
const token = session?.accessToken;
if (token) {
config.headers.set('Authorization', `Bearer ${token}`);
}
// Set Accept-Language header based on current locale
const locale = getLocale();
config.headers.set('Accept-Language', locale);
if (!(config.data instanceof FormData)) {
config.headers.set('Content-Type', 'application/json');
}
return config;
});
client.interceptors.response.use(
(response) => response,
async (error) => {
if (error.response?.status === 401) {
// Zaten giriş sayfasında değilsek veya auth ile ilgili bir istek değilse çıkış yap
const isAuthPath =
typeof window !== 'undefined' &&
(window.location.pathname.includes('/api/auth') || window.location.pathname === '/');
if (!isAuthPath) {
await signOut({ redirect: true, callbackUrl: '/' });
}
}
return Promise.reject(error);
},
);
return client;
}