fix(api): fix token handler
This commit is contained in:
31
api/fetch.ts
31
api/fetch.ts
@@ -17,6 +17,7 @@ fetch 参数:
|
||||
"use client"
|
||||
|
||||
import { useUserStore } from "@/app/store/user"
|
||||
import { usePermissionStore } from "@/components/label/store/auth"
|
||||
import { deleteCookie, refreshKey } from "@/components/login/api"
|
||||
import { isNumber } from "@/libs/util"
|
||||
|
||||
@@ -40,6 +41,7 @@ interface OptionsProps {
|
||||
|
||||
async function httpfetch<T>(options: OptionsProps): Promise<T> {
|
||||
const { removeUserInfo, refreshToken } = useUserStore.getState()
|
||||
const { logout } = usePermissionStore.getState()
|
||||
|
||||
const access_token = useUserStore.getState().access_token
|
||||
const refresh_token = useUserStore.getState().refresh_token
|
||||
@@ -186,24 +188,19 @@ async function httpfetch<T>(options: OptionsProps): Promise<T> {
|
||||
return options.isDownload
|
||||
? retry_response.blob()
|
||||
: retry_response.json()
|
||||
} else {
|
||||
const { code } = refreshResponse
|
||||
if (code === 406) {
|
||||
try {
|
||||
await deleteCookie()
|
||||
} catch (error) {
|
||||
rejected(error)
|
||||
} finally {
|
||||
// 清空身份信息并跳转至登录页面
|
||||
removeUserInfo()
|
||||
window.location.href = "/login"
|
||||
}
|
||||
} else {
|
||||
throw new Error("重新获取token失败!")
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
rejected(error)
|
||||
try {
|
||||
await deleteCookie()
|
||||
} catch (error) {
|
||||
rejected(error)
|
||||
} finally {
|
||||
// 清空身份信息并跳转至登录页面
|
||||
removeUserInfo()
|
||||
logout()
|
||||
window.location.href = "/login"
|
||||
}
|
||||
}
|
||||
} else if (status === 403) {
|
||||
if (
|
||||
@@ -271,8 +268,7 @@ async function httpfetch<T>(options: OptionsProps): Promise<T> {
|
||||
? retry_response.blob()
|
||||
: retry_response.json()
|
||||
} else {
|
||||
const { code } = refreshResponse
|
||||
if (code === 406) {
|
||||
if (refreshResponse.code === 406) {
|
||||
try {
|
||||
await deleteCookie()
|
||||
} catch (error) {
|
||||
@@ -280,6 +276,7 @@ async function httpfetch<T>(options: OptionsProps): Promise<T> {
|
||||
} finally {
|
||||
// 清空身份信息并跳转至登录页面
|
||||
removeUserInfo()
|
||||
logout()
|
||||
window.location.href = "/login"
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -46,32 +46,45 @@ export async function POST(req: any) {
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
const userData = await genAccessToken(req)
|
||||
|
||||
const authorization = `bearer ${userData?.access_token}`
|
||||
let fetchOptions: any = {}
|
||||
|
||||
const fetchOptions: any = {
|
||||
method,
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
authorization: authorization,
|
||||
},
|
||||
}
|
||||
|
||||
if (method === "POST" || method === "PUT") {
|
||||
let refreshBody = JSON.stringify({
|
||||
...JSON.parse(data),
|
||||
token: userData?.refresh_token || "",
|
||||
})
|
||||
fetchOptions.body = isRefresh ? refreshBody : data
|
||||
if (isRefresh) {
|
||||
if (!userData?.refresh_token) {
|
||||
return NextResponse.json(
|
||||
{ message: "Refresh token is empty", code: 406 },
|
||||
{ status: 200 }
|
||||
)
|
||||
}
|
||||
fetchOptions = {
|
||||
method,
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
token: userData?.refresh_token,
|
||||
}),
|
||||
}
|
||||
} else {
|
||||
if (isParamsValid(data)) {
|
||||
let test = ""
|
||||
for (const [key, value] of Object.entries(data)) {
|
||||
test += `&${key}=${value}`
|
||||
const authorization = `bearer ${userData?.access_token}`
|
||||
fetchOptions = {
|
||||
method,
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
authorization: authorization,
|
||||
},
|
||||
}
|
||||
if (method === "POST" || method === "PUT") {
|
||||
fetchOptions.body = data
|
||||
} else {
|
||||
if (isParamsValid(data)) {
|
||||
let test = ""
|
||||
for (const [key, value] of Object.entries(data)) {
|
||||
test += `&${key}=${value}`
|
||||
}
|
||||
url += `?${test}`
|
||||
}
|
||||
url += `?${test}`
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ import headerLinkClasses from "./HeaderLink.module.css"
|
||||
|
||||
import { useUserStore } from "@/app/store/user"
|
||||
import { usePathname, useRouter } from "next/navigation"
|
||||
import { usePermissionStore } from "../label/store/auth"
|
||||
import { deleteCookie } from "../login/api"
|
||||
import { MenuItem, withoutLayoutRoutes } from "./common"
|
||||
import { ClientIcon } from "./components/ClientIcon"
|
||||
@@ -140,6 +141,7 @@ export default function AppLayout({
|
||||
|
||||
const handleLogout = async () => {
|
||||
useUserStore.getState().removeUserInfo()
|
||||
usePermissionStore.getState().logout()
|
||||
process.env.NEXT_PUBLIC_OUTPUT_TYPE === "standalone" &&
|
||||
(await deleteCookie())
|
||||
router.push("/login")
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { ResultData } from "@/api/typing"
|
||||
import { Login } from "@/components/login/api/types"
|
||||
import redis from "@/components/login/libs/redis"
|
||||
import { encrypt, generateKeyFromEnv } from "@/components/login/libs/session"
|
||||
import { cookies } from "next/headers"
|
||||
import { Login } from "@/components/login/api/types"
|
||||
|
||||
export const handleLoginData = async (props: {
|
||||
clientIP: string
|
||||
@@ -75,8 +75,10 @@ export const handleRefreshToken = async (props: {
|
||||
...data,
|
||||
message: {
|
||||
...data.message,
|
||||
access_token: "",
|
||||
refresh_token: "",
|
||||
// access_token: "",
|
||||
// refresh_token: "",
|
||||
access: "",
|
||||
refresh: "",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user