mirror of
https://github.com/Radiquum/AniX.git
synced 2025-04-08 17:24:39 +00: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
|
@ -2,13 +2,18 @@ from fastapi import APIRouter
|
|||
from fastapi import Request
|
||||
from modules.proxy import apiRequest
|
||||
from modules.proxy import ENDPOINTS
|
||||
from typing import Union
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/{user_id}", summary="Get user profile by user ID")
|
||||
async def getUserById(request: Request, user_id: str, short: bool = False):
|
||||
res = await apiRequest(request, ENDPOINTS["profile"], user_id)
|
||||
async def getUserById(request: Request, user_id: str, short: bool = False, token: Union[None, str] = None):
|
||||
query = ""
|
||||
if token:
|
||||
query = f"?token={token}"
|
||||
|
||||
res = await apiRequest(request, ENDPOINTS["profile"], user_id, query=query)
|
||||
if short is False:
|
||||
return res
|
||||
return {
|
||||
|
@ -18,4 +23,5 @@ async def getUserById(request: Request, user_id: str, short: bool = False):
|
|||
"login": res["profile"]["login"],
|
||||
"avatar": res["profile"]["avatar"],
|
||||
},
|
||||
"is_my_profile": res["is_my_profile"]
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
|
|
@ -30,11 +30,11 @@ export default function Home() {
|
|||
useEffect(() => {
|
||||
setList(searchParams.get("list") || "last");
|
||||
}, []);
|
||||
|
||||
|
||||
async function fetchData(list, page = 0) {
|
||||
const url = `${endpoints.index[list]}?page=${page}`;
|
||||
const data = await getData(url);
|
||||
|
||||
|
||||
// Handle initial load (page 0) or subsequent pagination
|
||||
if (page === 0) {
|
||||
setReleases(data.content);
|
||||
|
@ -49,7 +49,7 @@ export default function Home() {
|
|||
setPage(0);
|
||||
fetchData(list); // Call fetchData here
|
||||
}, [list]);
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
if (list && releases) {
|
||||
fetchData(list, page); // Use fetchData for pagination
|
||||
|
|
31
frontend/app/store/user-store.js
Normal file
31
frontend/app/store/user-store.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
"use client";
|
||||
import { create } from "zustand";
|
||||
import { getJWT, setJWT, removeJWT, getMe } from "@/app/api/api-utils";
|
||||
import { endpoints } from "@/app/api/config";
|
||||
|
||||
export const useUserStore = create((set, get) => ({
|
||||
isAuth: false,
|
||||
user: null,
|
||||
token: null,
|
||||
login: (user, token) => {
|
||||
set({ isAuth: true, user, token });
|
||||
setJWT(token);
|
||||
},
|
||||
logout: () => {
|
||||
set({ isAuth: false, user: null, token: null });
|
||||
removeJWT();
|
||||
},
|
||||
checkAuth: async (user_id) => {
|
||||
const jwt = getJWT();
|
||||
if (jwt) {
|
||||
const me = await getMe(`${endpoints.profile}/${user_id}`, jwt);
|
||||
if (me.is_my_profile) {
|
||||
get().login(me, jwt);
|
||||
} else {
|
||||
get().logout();
|
||||
}
|
||||
} else {
|
||||
get().logout();
|
||||
}
|
||||
},
|
||||
}));
|
Loading…
Add table
Reference in a new issue