Добавлен frontend API-клиент авторизации

This commit is contained in:
2026-08-09 14:23:28 +03:00
parent c067b19059
commit 20e59131f2
+41
View File
@@ -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}` },
});
}