未认证时进入登录页面

This commit is contained in:
JIANG
2026-03-02 11:34:07 +08:00
parent 6c5862f7e4
commit cd34e511ac
+16 -1
View File
@@ -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;
};