更新登录功能

This commit is contained in:
Eamon
2026-08-13 18:28:20 +08:00
parent 115ee3bfeb
commit 4235458bea
25 changed files with 1900 additions and 873 deletions

View File

@@ -1,57 +1,10 @@
import { useEffect, useState } from "react"
import { useState } from "react"
import { Navigate, useLocation } from "react-router-dom"
import { getProfile, refreshAuth } from "@/api/auth"
import { isAuthed } from "@/api/token"
export default function AuthGuard({ children }: { children: React.ReactNode }) {
const location = useLocation()
const [isValid, setIsValid] = useState<boolean | null>(null)
useEffect(() => {
const checkAuth = async () => {
try {
const accessToken = localStorage.getItem("auth_token")
const refreshToken = localStorage.getItem("auth_refresh")
if (!accessToken && !refreshToken) {
setIsValid(false)
return
}
const profileResp = await getProfile()
if (profileResp.success) {
setIsValid(true)
return
}
if (refreshToken) {
const newTokens = await refreshAuth()
if (newTokens) {
setIsValid(true)
return
}
}
localStorage.removeItem("auth_token")
localStorage.removeItem("auth_refresh")
localStorage.removeItem("auth_expires_in")
setIsValid(false)
} catch {
localStorage.removeItem("auth_token")
localStorage.removeItem("auth_refresh")
localStorage.removeItem("auth_expires_in")
setIsValid(false)
}
}
checkAuth()
}, [])
if (isValid === null)
return (
<div className="flex justify-center items-center h-screen">
验证身份中...
</div>
)
const [isValid] = useState(isAuthed)
if (!isValid) {
return (