added security and fix edit profile
This commit is contained in:
@@ -9,6 +9,8 @@ import { API_URL } from "../config/authConstants";
|
||||
* id передан → PUT /api/v1/posts/:id (обновление)
|
||||
* id не передан → POST /api/v1/posts (создание)
|
||||
*
|
||||
* Автоматически добавляет заголовок Authorization с JWT-токеном.
|
||||
*
|
||||
* @returns {{
|
||||
* save: (id: string|null, data: FormData) => Promise<object|null>,
|
||||
* loading: boolean,
|
||||
@@ -28,9 +30,21 @@ export function usePostMutation() {
|
||||
setLoading(true);
|
||||
setError("");
|
||||
try {
|
||||
const token = localStorage.getItem("token");
|
||||
const url = id ? `${API_URL}/api/v1/posts/${id}` : `${API_URL}/api/v1/posts`;
|
||||
const method = id ? "PUT" : "POST";
|
||||
const res = await fetch(url, { method, body: data });
|
||||
const res = await fetch(url, {
|
||||
method,
|
||||
headers: {
|
||||
"Authorization": `Bearer ${token}`,
|
||||
},
|
||||
body: data,
|
||||
});
|
||||
|
||||
if (res.status === 401 || res.status === 403) {
|
||||
throw new Error("Требуется авторизация. Войдите в аккаунт.");
|
||||
}
|
||||
|
||||
if (!res.ok) throw new Error(`Ошибка сервера: ${res.status}`);
|
||||
return await res.json();
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user