未认证时进入登录页面
This commit is contained in:
+16
-1
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user