Archived
Добавлена сессионная авторизация frontend
This commit is contained in:
+36
-3
@@ -1,10 +1,25 @@
|
|||||||
const BASE = "/api";
|
const BASE = "/api";
|
||||||
|
const TOKEN_KEY = "access_token";
|
||||||
|
|
||||||
|
export function getToken() {
|
||||||
|
return sessionStorage.getItem(TOKEN_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setToken(token) {
|
||||||
|
sessionStorage.setItem(TOKEN_KEY, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function clearToken() {
|
||||||
|
sessionStorage.removeItem(TOKEN_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
async function request(path, options = {}) {
|
async function request(path, options = {}) {
|
||||||
|
const token = getToken();
|
||||||
const response = await fetch(`${BASE}${path}`, {
|
const response = await fetch(`${BASE}${path}`, {
|
||||||
...options,
|
...options,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||||
...(options.headers || {}),
|
...(options.headers || {}),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -31,11 +46,29 @@ export async function login(email, password) {
|
|||||||
});
|
});
|
||||||
const data = await response.json().catch(() => ({}));
|
const data = await response.json().catch(() => ({}));
|
||||||
if (!response.ok) throw new Error(data.detail || "Ошибка входа");
|
if (!response.ok) throw new Error(data.detail || "Ошибка входа");
|
||||||
|
setToken(data.access_token);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getMe(token) {
|
export function getMe() {
|
||||||
return request("/auth/me", {
|
return request("/auth/me");
|
||||||
headers: { Authorization: `Bearer ${token}` },
|
}
|
||||||
|
|
||||||
|
export function listTemplates() {
|
||||||
|
return request("/templates");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createTemplate(payload) {
|
||||||
|
return request("/templates", {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(payload),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function deactivateTemplate(id) {
|
||||||
|
return request(`/templates/${id}`, { method: "DELETE" });
|
||||||
|
}
|
||||||
|
|
||||||
|
export function listInstances() {
|
||||||
|
return request("/instances");
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user