mirror of
https://github.com/Radiquum/AniX.git
synced 2025-05-02 10:59:40 +05:00
front & back: getting ready for user auth
This commit is contained in:
parent
732799703d
commit
17b5693f34
4 changed files with 91 additions and 14 deletions
|
@ -1,11 +1,51 @@
|
|||
export const getData = async (url) => {
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (response.status !== 200) {
|
||||
throw new Error("Ошибка получения данных");
|
||||
}
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
return error;
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (response.status !== 200) {
|
||||
throw new Error("Ошибка получения данных");
|
||||
}
|
||||
};
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
export const authorize = async (url, data) => {
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
if (response.status !== 200) {
|
||||
throw new Error("Ошибка получения данных");
|
||||
}
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
export const getMe = async (url, jwt) => {
|
||||
try {
|
||||
const response = await fetch(`${url}?token=${jwt}&short=True`, {
|
||||
method: "GET",
|
||||
});
|
||||
if (response.status !== 200) {
|
||||
throw new Error("Ошибка получения данных");
|
||||
}
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
export function setJWT(jwt) {
|
||||
localStorage.setItem("jwt", jwt);
|
||||
}
|
||||
export function getJWT() {
|
||||
return localStorage.getItem("jwt");
|
||||
}
|
||||
export function removeJWT() {
|
||||
localStorage.removeItem("jwt");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue