mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 15:27:46 +00:00
Add frontend interceptor to improve user token expiration and redirect them back to login
This commit is contained in:
@@ -1,7 +1,15 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import { addSeconds, formatISO } from "date-fns";
|
||||||
|
|
||||||
|
import { createNotification } from "@app/components/notifications";
|
||||||
import SecurityClient from "@app/components/utilities/SecurityClient";
|
import SecurityClient from "@app/components/utilities/SecurityClient";
|
||||||
import { getAuthToken, getMfaTempToken, getSignupTempToken } from "@app/hooks/api/reactQuery";
|
import { SessionStorageKeys } from "@app/const";
|
||||||
|
import {
|
||||||
|
getAuthToken,
|
||||||
|
getMfaTempToken,
|
||||||
|
getSignupTempToken,
|
||||||
|
setAuthToken
|
||||||
|
} from "@app/hooks/api/reactQuery";
|
||||||
|
|
||||||
export const apiRequest = axios.create({
|
export const apiRequest = axios.create({
|
||||||
baseURL: "/",
|
baseURL: "/",
|
||||||
@@ -34,3 +42,57 @@ apiRequest.interceptors.request.use((config) => {
|
|||||||
|
|
||||||
return config;
|
return config;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let isRedirecting = false;
|
||||||
|
|
||||||
|
apiRequest.interceptors.response.use(
|
||||||
|
(response) => response,
|
||||||
|
async (error) => {
|
||||||
|
const { response } = error;
|
||||||
|
|
||||||
|
if (response && (response.status === 401 || response.status === 403)) {
|
||||||
|
const currentToken = getAuthToken();
|
||||||
|
const isAuthenticatedRequest = Boolean(currentToken);
|
||||||
|
|
||||||
|
if (isAuthenticatedRequest && !isRedirecting) {
|
||||||
|
// Check if the error indicates token expiration
|
||||||
|
const errorMessage = response.data?.message || "";
|
||||||
|
const isTokenExpired =
|
||||||
|
response.status === 401 ||
|
||||||
|
(errorMessage.toLowerCase().includes("token") &&
|
||||||
|
(errorMessage.toLowerCase().includes("expired") ||
|
||||||
|
errorMessage.toLowerCase().includes("invalid") ||
|
||||||
|
errorMessage.toLowerCase().includes("unauthorized")));
|
||||||
|
|
||||||
|
if (isTokenExpired) {
|
||||||
|
isRedirecting = true;
|
||||||
|
|
||||||
|
setAuthToken("");
|
||||||
|
SecurityClient.setToken("");
|
||||||
|
|
||||||
|
createNotification({
|
||||||
|
type: "error",
|
||||||
|
title: "Session Expired",
|
||||||
|
text: "Your session has expired. Redirecting to login page..."
|
||||||
|
});
|
||||||
|
|
||||||
|
sessionStorage.setItem(
|
||||||
|
SessionStorageKeys.ORG_LOGIN_SUCCESS_REDIRECT_URL,
|
||||||
|
JSON.stringify({
|
||||||
|
expiry: formatISO(addSeconds(new Date(), 300)), // 5 minutes
|
||||||
|
data: window.location.href
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
window.location.href = "/login";
|
||||||
|
}, 5000); // 5 seconds to read the notification
|
||||||
|
|
||||||
|
return Promise.reject(new Error("Session expired - redirecting to login"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user