增加鉴权

This commit is contained in:
xmdhs 2023-10-04 20:49:38 +08:00
parent db26c46a5e
commit c8c0d77165
No known key found for this signature in database
GPG Key ID: E809D6D43DEFCC95
5 changed files with 33 additions and 16 deletions

View File

@ -1,4 +1,4 @@
import { Routes, Route, createBrowserRouter, RouterProvider } from "react-router-dom";
import { Routes, Route, createBrowserRouter, RouterProvider, useNavigate } from "react-router-dom";
import { ScrollRestoration } from "react-router-dom";
import Login from '@/views/Login'
import Register from '@/views/Register'
@ -6,6 +6,8 @@ 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";
const router = createBrowserRouter([
{ path: "*", Component: Root },
@ -16,11 +18,14 @@ function Root() {
<>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<p>123</p>} />
<Route path="/*" element={<p>404</p>} />
<Route path="/login" element={<Login />} />
<Route path="/register" element={<Register />} />
<Route path="/profile" element={<Profile />} />
<Route path="/textures" element={<Textures />} />
<Route path="/security" element={<Security />} />
<Route path="/profile" element={<NeedLogin><Profile /></NeedLogin>} />
<Route path="/textures" element={<NeedLogin><Textures /></NeedLogin>} />
<Route path="/security" element={<NeedLogin><Security /></NeedLogin>} />
</Route>
</Routes>
<ScrollRestoration />
@ -38,3 +43,12 @@ export function PageRoute() {
}
function NeedLogin({ children }: { children: JSX.Element }) {
const t = useAtomValue(token)
const navigate = useNavigate();
if (t == "") {
navigate("/login")
return
}
return <> {children}</>
}

View File

@ -34,7 +34,8 @@ import Container from '@mui/material/Container';
import PersonIcon from '@mui/icons-material/Person';
import SecurityIcon from '@mui/icons-material/Security';
import SettingsIcon from '@mui/icons-material/Settings';
import useTilg from 'tilg'
import { Link } from "react-router-dom";
const drawerWidth = 240;
const DrawerOpen = atom(false)
@ -58,8 +59,6 @@ const Layout = memo(function Layout() {
const theme = useTheme();
const [err, setErr] = useAtom(LayoutAlertErr)
useTilg()
return (<>
<Box sx={{ display: 'flex' }}>
<AppBar position="fixed"
@ -113,7 +112,7 @@ const MyToolbar = memo(function MyToolbar() {
navigate("/login")
})
useTilg()
return (
<>
@ -132,7 +131,9 @@ const MyToolbar = memo(function MyToolbar() {
</>)
}
<Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
{server.data?.meta.serverName ?? "皮肤站"}
<Link to="/" style={{ color: "unset", textDecoration: "unset" }}>
{server.data?.meta.serverName ?? "皮肤站"}
</Link>
</Typography>
{nowUser.name != "" && (
<div>
@ -173,7 +174,7 @@ const MyToolbar = memo(function MyToolbar() {
})
const MyList = memo(function MyList(p: { list: ListItem[] }) {
useTilg()
return (
<>
@ -251,7 +252,7 @@ const MyDrawer = function MyDrawer() {
}
] as ListItem[], [])
useTilg()
return (<>
{userinfo.data && (

View File

@ -17,7 +17,6 @@ import type { ReactSkinview3dOptions } from "react-skinview3d"
import { WalkingAnimation } from "skinview3d"
import type { SkinViewer } from "skinview3d"
import Skeleton from '@mui/material/Skeleton';
import useTilg from 'tilg';
import useTitle from '@/hooks/useTitle';
const Profile = function Profile() {
@ -45,7 +44,7 @@ const Profile = function Profile() {
}, [SkinInfo.data])
useTilg()
return (
<>

View File

@ -7,10 +7,11 @@ import { useEffect, useState } from "react";
import { produce } from 'immer'
import { changePasswd } from "@/apis/apis";
import { useAtom, useSetAtom } from "jotai";
import { LayoutAlertErr, token } from "@/store/store";
import { LayoutAlertErr, token, user } from "@/store/store";
import Loading from "@/components/Loading";
import { ApiErr } from "@/apis/error";
import { useNavigate } from "react-router-dom";
import useTitle from "@/hooks/useTitle";
export default function Security() {
const [pass, setPass] = useState({
@ -23,7 +24,9 @@ export default function Security() {
const [nowToken, setToken] = useAtom(token)
const [load, setLoad] = useState(false)
const setLayoutErr = useSetAtom(LayoutAlertErr)
const setUser = useSetAtom(user)
const navigate = useNavigate();
useTitle("安全设置")
useEffect(() => {
if (pass.pass1 != pass.pass2 && pass.pass2 != "") {
@ -43,7 +46,7 @@ export default function Security() {
return
}
setLayoutErr(String(e))
}).finally(() => setLoad(false)).then(() => [navigate("/login"), setToken("no")])
}).finally(() => setLoad(false)).then(() => [navigate("/login"), setToken(""), setUser({ name: "", uuid: "" })])
}

View File

@ -81,7 +81,7 @@ const Textures = function Textures() {
finally(() => setLoading(false))
}
useTilg()
return (<>
<Box sx={{