mirror of
https://github.com/Buriburizaem0n/admin-frontend-domain.git
synced 2026-09-19 17:50:13 +00:00
feat: server transfer rotation
This commit is contained in:
+10
-10
@@ -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>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user