From cd34e511acdb728acad7e154dde8c5024c801a94 Mon Sep 17 00:00:00 2001 From: JIANG Date: Mon, 2 Mar 2026 11:34:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AA=E8=AE=A4=E8=AF=81=E6=97=B6=E8=BF=9B?= =?UTF-8?q?=E5=85=A5=E7=99=BB=E5=BD=95=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/apiFetch.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/lib/apiFetch.ts b/src/lib/apiFetch.ts index c303987..08cdf49 100644 --- a/src/lib/apiFetch.ts +++ b/src/lib/apiFetch.ts @@ -1,5 +1,9 @@ import { useProjectStore } from "@/store/projectStore"; import { getAccessToken } from "@/lib/authToken"; +import { signOut } from "next-auth/react"; +import { useAuthStore } from "@/store/authStore"; + +let isSigningOut = false; const resolveUrl = (input: RequestInfo | URL) => { if (typeof input === "string") return input; @@ -24,5 +28,16 @@ export const apiFetch = async ( if (projectId && !isMetaProjectsRequest(input)) { headers.set("X-Project-Id", projectId); } - return fetch(input, { ...init, headers }); + + const response = await fetch(input, { ...init, headers }); + + if (response.status === 401 && typeof window !== "undefined") { + useAuthStore.getState().setAccessToken(null); + if (!isSigningOut) { + isSigningOut = true; + await signOut({ redirect: true, callbackUrl: "/login" }); + } + } + + return response; };