api requests
This commit is contained in:
parent
fe74490440
commit
5a1cc7c43c
22 changed files with 665 additions and 133 deletions
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue