新增请求未认证时,触发登陆状态变更操作
This commit is contained in:
@@ -2,6 +2,8 @@ import axios from "axios";
|
|||||||
import { config } from "@config/config";
|
import { config } from "@config/config";
|
||||||
import { useProjectStore } from "@/store/projectStore";
|
import { useProjectStore } from "@/store/projectStore";
|
||||||
import { getAccessToken } from "@/lib/authToken";
|
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;
|
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,
|
baseURL: API_URL,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let isSigningOut = false;
|
||||||
|
|
||||||
const isMetaProjectsRequest = (request: {
|
const isMetaProjectsRequest = (request: {
|
||||||
baseURL?: string;
|
baseURL?: string;
|
||||||
url?: string;
|
url?: string;
|
||||||
@@ -32,3 +36,17 @@ api.interceptors.request.use(async (request) => {
|
|||||||
|
|
||||||
return 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);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user