diff --git a/frontend/src/Route.tsx b/frontend/src/Route.tsx index 86235a3..ef46d14 100644 --- a/frontend/src/Route.tsx +++ b/frontend/src/Route.tsx @@ -1,4 +1,4 @@ -import { Routes, Route, createBrowserRouter, RouterProvider, useNavigate, Outlet, Navigate } from "react-router-dom"; +import { Routes, Route, createBrowserRouter, RouterProvider, Outlet } from "react-router-dom"; import { ScrollRestoration } from "react-router-dom"; import Login from '@/views/Login' import Register from '@/views/Register' @@ -6,12 +6,9 @@ import Profile from '@/views/profile/Profile' import Textures from '@/views/profile/Textures' import Security from '@/views/profile/Security' import Layout from '@/views/Layout' -import { useAtomValue } from "jotai"; -import { token } from "@/store/store"; -import { ApiErr } from "@/apis/error"; -import { userInfo } from "@/apis/apis"; -import { useRequest } from "ahooks"; import UserAdmin from "@/views/admin/UserAdmin"; +import NeedLogin from "@/components/NeedLogin"; +import Index from "@/views/Index"; const router = createBrowserRouter([ { path: "*", Component: Root }, @@ -22,7 +19,7 @@ function Root() { <> }> - + } /> 404

} /> } /> } /> @@ -53,35 +50,3 @@ export function PageRoute() { ) } - -function NeedLogin({ children, needAdmin = false }: { children: JSX.Element, needAdmin?: boolean }) { - const t = useAtomValue(token) - const navigate = useNavigate(); - useRequest(() => userInfo(t), { - refreshDeps: [t], - cacheKey: "/api/v1/user" + t, - staleTime: 60000, - onError: e => { - if (e instanceof ApiErr && e.code == 5) { - navigate("/login") - } - console.warn(e) - }, - onSuccess: u => { - if (!u) return - if (!u.is_admin && needAdmin) { - navigate("/login") - } - if (u.uuid == "") { - navigate("/login") - } - } - }) - - if (!localStorage.getItem("token") || localStorage.getItem("token") == '""') { - return - } - - - return <> {children} -} \ No newline at end of file diff --git a/frontend/src/components/NeedLogin.tsx b/frontend/src/components/NeedLogin.tsx new file mode 100644 index 0000000..20c26f4 --- /dev/null +++ b/frontend/src/components/NeedLogin.tsx @@ -0,0 +1,39 @@ +import { userInfo } from "@/apis/apis"; +import { ApiErr } from "@/apis/error"; +import { token } from "@/store/store"; +import { useRequest } from "ahooks"; +import { useAtomValue } from "jotai"; +import { useNavigate, Navigate } from "react-router-dom"; + + +export default function NeedLogin({ children, needAdmin = false }: { children: JSX.Element, needAdmin?: boolean }) { + const t = useAtomValue(token) + const navigate = useNavigate(); + useRequest(() => userInfo(t), { + refreshDeps: [t], + cacheKey: "/api/v1/user" + t, + staleTime: 60000, + onError: e => { + if (e instanceof ApiErr && e.code == 5) { + navigate("/login") + } + console.warn(e) + }, + onSuccess: u => { + if (!u) return + if (!u.is_admin && needAdmin) { + navigate("/login") + } + if (u.uuid == "") { + navigate("/login") + } + } + }) + + if (!localStorage.getItem("token") || localStorage.getItem("token") == '""') { + return + } + + + return <> {children} +} \ No newline at end of file diff --git a/frontend/src/views/Index.tsx b/frontend/src/views/Index.tsx new file mode 100644 index 0000000..1fa3c96 --- /dev/null +++ b/frontend/src/views/Index.tsx @@ -0,0 +1,9 @@ +import Profile from "@/views/profile/Profile" +import Login from "@/views/Login" + +export default function Index() { + if (!(localStorage.getItem("token") || localStorage.getItem("token") == '""')) { + return + } + return +} \ No newline at end of file