fix: Improve token refresh logic and API interceptor handling (#7158)

* 🐛 (api.tsx): fix logic to correctly handle refreshing access token and retrying requests on authentication errors
🐛 (use-post-refresh-access.ts): fix retry logic to always retry refreshing access token regardless of auto login setting

* ✨ (use-get-autologin.ts): add support for autoLogin state in useGetAutoLogin hook to handle auto login scenarios more effectively
📝 (use-post-refresh-access.ts): import and use autoLogin state from authStore in useRefreshAccessToken hook to determine retry count based on auto login status
This commit is contained in:
Cristhian Zanforlin Lousa 2025-03-19 10:20:55 -03:00 • committed by GitHub
commit 828e9eab78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 30 additions and 20 deletions

View file

@ -65,8 +65,11 @@ function ApiInterceptor() {
const isAuthenticationError = const isAuthenticationError =
error?.response?.status === 403 || error?.response?.status === 401; error?.response?.status === 403 || error?.response?.status === 401;
if (isAuthenticationError && !IS_AUTO_LOGIN) { const shouldRetryRefresh =
if (autoLogin !== undefined && !autoLogin) { (isAuthenticationError && !IS_AUTO_LOGIN) ||
(isAuthenticationError && !autoLogin && autoLogin !== undefined);
if (shouldRetryRefresh) {
if ( if (
error?.config?.url?.includes("github") || error?.config?.url?.includes("github") ||
error?.config?.url?.includes("public") error?.config?.url?.includes("public")
@ -85,7 +88,6 @@ function ApiInterceptor() {
return Promise.reject(error); return Promise.reject(error);
} }
} }
}
await clearBuildVerticesState(error); await clearBuildVerticesState(error);

View file

@ -31,6 +31,7 @@ export const useGetAutoLogin: useQueryFunctionType<undefined, undefined> = (
const isLoginPage = location.pathname.includes("login"); const isLoginPage = location.pathname.includes("login");
const navigate = useCustomNavigate(); const navigate = useCustomNavigate();
const { mutateAsync: mutationLogout } = useLogout(); const { mutateAsync: mutationLogout } = useLogout();
const autoLogin = useAuthStore((state) => state.autoLogin);
const retryCountRef = useRef(0); const retryCountRef = useRef(0);
const retryTimerRef = useRef<NodeJS.Timeout | null>(null); const retryTimerRef = useRef<NodeJS.Timeout | null>(null);
@ -67,8 +68,13 @@ export const useGetAutoLogin: useQueryFunctionType<undefined, undefined> = (
}; };
const handleAutoLoginError = async () => { const handleAutoLoginError = async () => {
const manualLoginNotAuthenticated = !isAuthenticated && !IS_AUTO_LOGIN; const manualLoginNotAuthenticated =
const autoLoginNotAuthenticated = !isAuthenticated && IS_AUTO_LOGIN; (!isAuthenticated && !IS_AUTO_LOGIN) ||
(!isAuthenticated && autoLogin !== undefined && !autoLogin);
const autoLoginNotAuthenticated =
(!isAuthenticated && IS_AUTO_LOGIN) ||
(!isAuthenticated && autoLogin !== undefined && autoLogin);
if (manualLoginNotAuthenticated) { if (manualLoginNotAuthenticated) {
await mutationLogout(); await mutationLogout();

View file

@ -1,4 +1,5 @@
import { IS_AUTO_LOGIN, LANGFLOW_REFRESH_TOKEN } from "@/constants/constants"; import { IS_AUTO_LOGIN, LANGFLOW_REFRESH_TOKEN } from "@/constants/constants";
import useAuthStore from "@/stores/authStore";
import { useMutationFunctionType } from "@/types/api"; import { useMutationFunctionType } from "@/types/api";
import { Cookies } from "react-cookie"; import { Cookies } from "react-cookie";
import { api } from "../../api"; import { api } from "../../api";
@ -17,6 +18,7 @@ export const useRefreshAccessToken: useMutationFunctionType<
> = (options?) => { > = (options?) => {
const { mutate } = UseRequestProcessor(); const { mutate } = UseRequestProcessor();
const cookies = new Cookies(); const cookies = new Cookies();
const autoLogin = useAuthStore((state) => state.autoLogin);
async function refreshAccess(): Promise<IRefreshAccessToken> { async function refreshAccess(): Promise<IRefreshAccessToken> {
const res = await api.post<IRefreshAccessToken>(`${getURL("REFRESH")}`); const res = await api.post<IRefreshAccessToken>(`${getURL("REFRESH")}`);
@ -27,7 +29,7 @@ export const useRefreshAccessToken: useMutationFunctionType<
const mutation = mutate(["useRefreshAccessToken"], refreshAccess, { const mutation = mutate(["useRefreshAccessToken"], refreshAccess, {
...options, ...options,
retry: IS_AUTO_LOGIN ? 0 : 3, retry: IS_AUTO_LOGIN || autoLogin ? 0 : 2,
}); });
return mutation; return mutation;