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