diff --git a/frontend/src/api.js b/frontend/src/api.js new file mode 100644 index 0000000..ced1396 --- /dev/null +++ b/frontend/src/api.js @@ -0,0 +1,41 @@ +const BASE = "/api"; + +async function request(path, options = {}) { + const response = await fetch(`${BASE}${path}`, { + ...options, + headers: { + "Content-Type": "application/json", + ...(options.headers || {}), + }, + }); + const data = await response.json().catch(() => ({})); + if (!response.ok) { + throw new Error(data.detail || "Ошибка запроса"); + } + return data; +} + +export async function register(email, password) { + return request("/auth/register", { + method: "POST", + body: JSON.stringify({ email, password }), + }); +} + +export async function login(email, password) { + const body = new URLSearchParams({ username: email, password }); + const response = await fetch(`${BASE}/auth/login`, { + method: "POST", + headers: { "Content-Type": "application/x-www-form-urlencoded" }, + body, + }); + const data = await response.json().catch(() => ({})); + if (!response.ok) throw new Error(data.detail || "Ошибка входа"); + return data; +} + +export function getMe(token) { + return request("/auth/me", { + headers: { Authorization: `Bearer ${token}` }, + }); +}