src/api/auth.jsjavascript · @kibot
20/9/2026, 17:42:25const API = "http://localhost:8000";
function getToken() {
return localStorage.getItem("token");
}
async function request(path, options = {}) {
const headers = {
"Content-Type": "application/json",
...(options.headers || {}),
};
const token = getToken();
if (token) {
headers["Authorization"] = `Bearer ${token}`;
}
const res = await fetch(`${API}${path}`, { ...options, headers });
if (!res.ok) {
const data = await res.json().catch(() => ({}));
throw new Error(data.detail || `Error ${res.status}`);
}
return res.json();
}
export const authApi = {
register: (email, password) =>
request("/auth/register", {
method: "POST",
body: JSON.stringify({ email, password }),
}),
login: (email, password) =>
request("/auth/login", {
method: "POST",
body: JSON.stringify({ email, password }),
}),
me: () => request("/auth/me"),
};