新增请求未认证时,触发登陆状态变更操作

This commit is contained in:
JIANG
2026-02-27 17:19:41 +08:00
parent f9dc4b74d0
commit 2d27e803a3
+18
View File
@@ -2,6 +2,8 @@ import axios from "axios";
import { config } from "@config/config";
import { useProjectStore } from "@/store/projectStore";
import { getAccessToken } from "@/lib/authToken";
import { signOut } from "next-auth/react";
import { useAuthStore } from "@/store/authStore";
export const API_URL = process.env.NEXT_PUBLIC_API_URL || config.BACKEND_URL;
@@ -9,6 +11,8 @@ export const api = axios.create({
baseURL: API_URL,
});
let isSigningOut = false;
const isMetaProjectsRequest = (request: {
baseURL?: string;
url?: string;
@@ -32,3 +36,17 @@ api.interceptors.request.use(async (request) => {
return request;
});
api.interceptors.response.use(
(response) => response,
async (error) => {
if (error?.response?.status === 401 && typeof window !== "undefined") {
useAuthStore.getState().setAccessToken(null);
if (!isSigningOut) {
isSigningOut = true;
await signOut({ redirect: true, callbackUrl: "/login" });
}
}
return Promise.reject(error);
},
);