feat: server transfer rotation

This commit is contained in:
naiba
2026-05-25 10:15:24 +00:00
parent 2010d80914
commit 849d131382
60 changed files with 4488 additions and 1359 deletions
+10 -10
View File
@@ -1,6 +1,6 @@
import { getProfile, login as loginRequest } from "@/api/user"
import { AuthContextProps } from "@/types"
import { createContext, useContext, useEffect, useMemo } from "react"
import { createContext, useCallback, useContext, useEffect, useMemo } from "react"
import { useTranslation } from "react-i18next"
import { useNavigate } from "react-router-dom"
import { toast } from "sonner"
@@ -25,15 +25,15 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
const user = await getProfile()
user.role = user.role || 0
setProfile(user)
} catch (error: any) {
} catch {
setProfile(undefined)
}
})()
}, [])
}, [setProfile])
const navigate = useNavigate()
const login = async (username: string, password: string) => {
const login = useCallback(async (username: string, password: string) => {
try {
await loginRequest(username, password)
const user = await getProfile()
@@ -48,9 +48,9 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
toast(msg || t("NetworkError"))
}
}
}
}, [navigate, setProfile, t])
const loginOauth2 = async () => {
const loginOauth2 = useCallback(async () => {
try {
const user = await getProfile()
user.role = user.role || 0
@@ -61,9 +61,9 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
} finally {
window.history.replaceState({}, document.title, window.location.pathname)
}
}
}, [navigate, setProfile])
const logout = () => {
const logout = useCallback(() => {
document.cookie.split(";").forEach(function (c) {
document.cookie = c
.replace(/^ +/, "")
@@ -71,7 +71,7 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
})
setProfile(undefined)
navigate("/dashboard/login", { replace: true })
}
}, [navigate, setProfile])
const value = useMemo(
() => ({
@@ -80,7 +80,7 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
loginOauth2,
logout,
}),
[profile],
[profile, login, loginOauth2, logout],
)
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>
}