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:
parent
41ed0bb50b
commit
828e9eab78
3 changed files with 30 additions and 20 deletions
|
|
@ -65,25 +65,27 @@ 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) ||
|
||||||
if (
|
(isAuthenticationError && !autoLogin && autoLogin !== undefined);
|
||||||
error?.config?.url?.includes("github") ||
|
|
||||||
error?.config?.url?.includes("public")
|
|
||||||
) {
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
const stillRefresh = checkErrorCount();
|
|
||||||
if (!stillRefresh) {
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
await tryToRenewAccessToken(error);
|
if (shouldRetryRefresh) {
|
||||||
|
if (
|
||||||
|
error?.config?.url?.includes("github") ||
|
||||||
|
error?.config?.url?.includes("public")
|
||||||
|
) {
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
const stillRefresh = checkErrorCount();
|
||||||
|
if (!stillRefresh) {
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
|
||||||
const accessToken = cookies.get(LANGFLOW_ACCESS_TOKEN);
|
await tryToRenewAccessToken(error);
|
||||||
if (!accessToken && error?.config?.url?.includes("login")) {
|
|
||||||
return Promise.reject(error);
|
const accessToken = cookies.get(LANGFLOW_ACCESS_TOKEN);
|
||||||
}
|
if (!accessToken && error?.config?.url?.includes("login")) {
|
||||||
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue