front & back: getting ready for user auth

This commit is contained in:
Kentai Radiquum 2024-04-21 02:09:40 +05:00
parent 732799703d
commit 17b5693f34
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
4 changed files with 91 additions and 14 deletions

View file

@ -2,13 +2,18 @@ from fastapi import APIRouter
from fastapi import Request from fastapi import Request
from modules.proxy import apiRequest from modules.proxy import apiRequest
from modules.proxy import ENDPOINTS from modules.proxy import ENDPOINTS
from typing import Union
router = APIRouter() router = APIRouter()
@router.get("/{user_id}", summary="Get user profile by user ID") @router.get("/{user_id}", summary="Get user profile by user ID")
async def getUserById(request: Request, user_id: str, short: bool = False): async def getUserById(request: Request, user_id: str, short: bool = False, token: Union[None, str] = None):
res = await apiRequest(request, ENDPOINTS["profile"], user_id) query = ""
if token:
query = f"?token={token}"
res = await apiRequest(request, ENDPOINTS["profile"], user_id, query=query)
if short is False: if short is False:
return res return res
return { return {
@ -18,4 +23,5 @@ async def getUserById(request: Request, user_id: str, short: bool = False):
"login": res["profile"]["login"], "login": res["profile"]["login"],
"avatar": res["profile"]["avatar"], "avatar": res["profile"]["avatar"],
}, },
"is_my_profile": res["is_my_profile"]
} }

View file

@ -8,4 +8,44 @@ export const getData = async (url) => {
} catch (error) { } catch (error) {
return 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");
}

View 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();
}
},
}));