Files
dupe/client/src/utils/api.js

20 lines
734 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export const API = import.meta.env.VITE_API || "http://dupe.panda";
// 🔐 Ortak kimlik doğrulama başlığı (token varsa ekler)
export function authHeaders() {
const token = localStorage.getItem("token");
return token ? { Authorization: `Bearer ${token}` } : {};
}
// 🔧 Yardımcı fetch (otomatik token ekler, hata durumunda logout)
export async function apiFetch(path, options = {}) {
const headers = { ...(options.headers || {}), ...authHeaders() };
const res = await fetch(`${API}${path}`, { ...options, headers });
// Token süresi dolmuşsa veya yanlışsa kullanıcıyı çıkışa yönlendir
if (res.status === 401) {
localStorage.removeItem("token");
window.location.reload();
}
return res;
}