api requests

This commit is contained in:
Tatiana Nikolaeva 2025-05-22 20:23:09 +05:00
parent fe74490440
commit 5a1cc7c43c
22 changed files with 665 additions and 133 deletions

View file

@ -10,6 +10,45 @@ interface IRegistrationData extends IAuthData{
firstName: string;
lastName: string;
}
//
// interface IUserData{
// username: string;
// firstName: string;
// lastName: string;
// email: string;
// }
export const getCurrentUser = async (): Promise<IRegistrationData> => {
const token = localStorage.getItem("token");
if (!token) {
throw new Error("Токен отсутствует");
}
try {
const response = await fetch(`${BASE_URL}/auth/me`, {
...createRequestConfig('GET'),
headers: {
'Authorization': `Bearer ${token}`
}
});
if (response.status === 401) {
localStorage.removeItem("token");
throw new Error("Сессия истекла. Пожалуйста, войдите снова.");
}
if (!response.ok) {
throw new Error(`Ошибка сервера: ${response.status}`);
}
const userData = await handleResponse(response);
localStorage.setItem("user", JSON.stringify(userData));
return userData;
} catch (error) {
console.error("Ошибка при получении данных пользователя:", error);
throw error;
}
};
export const registerUser = async (data: IRegistrationData) => {
try{
@ -60,4 +99,4 @@ export const authUser = async (data: IAuthData) => {
console.error("Login error:", error);
throw error;
}
};
};